Есть класс :
[Serializable]
public class UserAccount
{
public string Name { get; set; }
public string Surname { get; set; }
public string Login { get; set; }
public PasswordBox Password { get; set; }
public UserAccount(string _name, string _surname, string _login, PasswordBox _password)
{
Name = _name;
Surname = _surname;
Login = _login;
Password = _password;
}
}
Метод десериализации:
public void Deserialize()
{
try
{
FileStream fs = new FileStream("..\\..\\Accounts.dat", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
listUsers = (List<UserAccount>)formatter.Deserialize(fs);//вылетает exception
fs.Close();
}
catch
{
listUsers = new List<UserAccount>();
}
}
Ошибка:
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll Additional information: 'The invocation of the constructor on type 'Gallery.ViewModels.LoginViewModel' that matches the specified binding constraints threw an exception.' Line number '15' and line position '10'.
Update
public class LoginViewModel : INotifyPropertyChanged
{
private List<UserAccount> listUsers;
public LoginViewModel()
{
_currentName = String.Empty;
_currentSurname = String.Empty;
_currentLogin = String.Empty;
_currentPasswordBox = new PasswordBox();
Deserialize();
}
private string _currentSurname;
public string CurrentSurname
{
get
{
return _currentSurname;
}
set
{
_currentSurname = value;
OnPropertyChanged();
}
}
private string _currentName;
public string CurrentName
{
get
{
return _currentName;
}
set
{
_currentName = value;
OnPropertyChanged();
}
}
private string _currentLogin;
public string CurrentLogin
{
get
{
return _currentLogin;
}
set
{
_currentLogin = value;
OnPropertyChanged();
}
}
private PasswordBox _currentPasswordBox;
public PasswordBox currentPassword
{
get { return _currentPasswordBox; }
set
{
_currentPasswordBox = value;
OnPropertyChanged();
}
}
private RegistrationCheckCommand checkReg;
public ICommand ButtonClick
{
get { return checkReg ?? (checkReg = new RegistrationCheckCommand(o => AddNewAccount())); }
}
private void AddNewAccount()
{
UserAccount user = new UserAccount(CurrentName, CurrentSurname, CurrentLogin, currentPassword);
listUsers.Add(user);
Serialize();
MessageBox.Show("Account adding!");
}
public void Serialize()
{
try
{
FileStream fs = new FileStream("..\\..\\Accounts.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, listUsers);
fs.Close();
}
catch
{
}
}
public void Deserialize()
{
//try
//{
FileStream fs = new FileStream("..\\..\\Accounts.dat", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
listUsers = (List<UserAccount>)formatter.Deserialize(fs);
fs.Close();
//}
//catch
//{
// listUsers = new List<UserAccount>();
//}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Xaml
<Controls:MetroWindow x:Class="Gallery.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Gallery.ViewModels"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
mc:Ignorable="d"
Title="Login window" TitleCaps="False" Height="433.735" Width="300.803"
ResizeMode="NoResize"
BorderBrush="White">
<Controls:MetroWindow.Resources>
<local:LoginViewModel x:Key="LoginWindow"/>
</Controls:MetroWindow.Resources>
<Controls:MetroAnimatedTabControl HorizontalAlignment="Center" DataContext="{StaticResource LoginWindow}">
<Controls:MetroTabItem Header="Sign In">
<StackPanel VerticalAlignment="Center">
<TextBlock Text="Login" Foreground="White" FontSize="25" HorizontalAlignment="Center" ></TextBlock>
<TextBox Name="login" Controls:TextBoxHelper.ClearTextButton="True"></TextBox>
<TextBlock Text="Password" Foreground="White" FontSize="25" HorizontalAlignment="Center" ></TextBlock>
<PasswordBox Name="pass" Controls:TextBoxHelper.ClearTextButton="True"></PasswordBox>
<Button Style="{StaticResource AccentedSquareButtonStyle}"
Margin="0,20,0,0" Content="sign in"
IsEnabled="{Binding Text.Length, ElementName=login}">
</Button>
</StackPanel>
</Controls:MetroTabItem>
<Controls:MetroTabItem Header="Registration">
<StackPanel VerticalAlignment="Center">
<TextBlock Text="Login" Foreground="White" FontSize="20"></TextBlock>
<TextBox Name="regLogin" Controls:TextBoxHelper.ClearTextButton="True"></TextBox>
<TextBlock Text="Password" Foreground="White" FontSize="20" ></TextBlock>
<PasswordBox Controls:TextBoxHelper.ClearTextButton="True"></PasswordBox>
<TextBlock Text="Name" Foreground="White" FontSize="20"></TextBlock>
<TextBox Name="regName" Controls:TextBoxHelper.ClearTextButton="True"></TextBox>
<TextBlock Text="Surname" Foreground="White" FontSize="20"></TextBlock>
<TextBox Name="regSurname" Controls:TextBoxHelper.ClearTextButton="True"></TextBox>
<Button Style="{StaticResource AccentedSquareButtonStyle}"
Margin="0,20,0,0" Content="Register" Command="{Binding ButtonClick }">
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource RegistrationFormConverter}">
<Binding ElementName="regLogin" Path="Text.Length" />
<Binding ElementName="regName" Path="Text.Length" />
<Binding ElementName="regSurname" Path="Text.Length" />
</MultiBinding>
</Button.IsEnabled>
</Button>
</StackPanel>
</Controls:MetroTabItem>
</Controls:MetroAnimatedTabControl>
StackTrace
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at Gallery.ViewModels.LoginViewModel.Deserialize() in C:\Users\Константин\Documents\Visual Studio 2015\Projects\Gallery\Gallery\ViewModels\LoginViewModel.cs:line 121