Как разделить свойства класса и методы класса в MVVM - PullRequest
0 голосов
/ 28 июля 2011

В последний раз я задавал вопрос об использовании свойства в MVVM в моем приложении для Windows Phone 7. Я мог бы сделать отличные советы. Пожалуйста, смотрите мой предыдущий вопрос.

Невозможно привязать свойство текстового блока из другого класса к классу пользовательского интерфейса, используя MVVM

Благодаря моему кодированию свойства MVVM растут. Поэтому я хочу разделить свойства класса и методы. Но я не мог разделить это. Пожалуйста, дайте мне знать, как разделить свойства класса и методы класса в MVVM.

Мой код здесь.

Authentication.cs

public class Authentication : ViewModelBase
{
    private string _ErrorStatus;
    public string ErrorStatus
    {
        get
        {
            return _ErrorStatus;
        }
        set
        {
            _ErrorStatus = value;
            NotifyPropertyChanged("ErrorStatus");
        }
    }

    void Authenticate()
    {
        ErrorStatus = "Access Denied";
    }
}

Я хочу разделить вот так. Но «ErrorStatus» не изменяется.

Properties.cs

public class Properties : ViewModelBase
{
    private string _ErrorStatus;
    public string ErrorStatus
    {
        get
        {
            return _ErrorStatus;
        }
        set
        {
            _ErrorStatus = value;
            NotifyPropertyChanged("ErrorStatus");
        }
    }
}

Authentication.cs

public class Authentication
{
    Properties properties = new Properties();

    void Authenticate()
    {
        //not work
        properties.ErrorStatus = "Access Denied";
    }
}

Ответы [ 2 ]

1 голос
/ 28 июля 2011

Следующее позволяет Authentication иметь доступ к свойствам Properties и показывает, как оно может работать.

public class Properties : ViewModelBase
{
    private string _ErrorStatus;
    public string ErrorStatus
    {
        get
        {
            return _ErrorStatus;
        }
        set
        {
            _ErrorStatus = value;
            RaisePropertyChanged("ErrorStatus");
        }
    }
}

public class Authentication : Properties
{
    public void Authenticate()
    {
        ErrorStatus = "Access Denied";
    }
}

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();

        var test = new Authentication();

        test.Authenticate();

        MessageBox.Show(test.ErrorStatus); // Displays "Access Denied"
    }
}
0 голосов
/ 28 июля 2011

Я полагаю, у вас есть тот же старый

private Authentication authentication;  
public MainPage() 
{
     InitializeComponent();
     this.authentication = new Authentication();
     this.DataContext = authentication; 
}  
void btnAuthenticate_Click(object src, EventArgs e) 
{
     authentication.Authenticate(); 
} 

и

public class Authentication
{
    private string properties = new Properties();
    public string Properties
    {
        get
        {
            return properties;
        }
        set
        {
            properties = value;
            NotifyPropertyChanged("Properties");
        }
    }
    void Authenticate()
    {
        Properties.ErrorStatus = "Access Denied";
    }
}

нормальный xaml должен быть

<TextBlock Text="{Binding Path=Properties.ErrorStatus}" />

но иногда вы меняете экземпляр свойств. так что если это не сработает, вы можете попробовать этот xaml

<Border DataContext="{Binding Properties}">
     <TextBlock Text="{Binding Path=ErrorStatus}" /> 
</Border>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...