开发者

How to divide properties class and methods class in MVVM

开发者 https://www.devze.com 2023-03-23 08:52 出处:网络
Last time, I submitted a question about using property in MVVM in my Windows Phone 7 app. I could well done by excellent advices. Please see my previous question.

Last time, I submitted a question about using property in MVVM in my Windows Phone 7 app. I could well done by excellent advices. Please see my previous question.

Can not bind textblock property from another class to UI class using MVVM

Through my coding, MVVM properties are increasing. So I want to divide properties class and methods. But I couldn't divide it. Please let me k开发者_开发百科now how to divide properties class and methods class in MVVM.

My code is here.

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";
    }
}

I want to divide like this. But "ErrorStatus" is not changed.

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";
    }
}


The following enables Authentication to have access to properties of Properties and shows how it could work.

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"
    }
}


I assume you have the same old

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

and

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";
    }
}

the normal xaml should then be

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

but sometimes, you change the Properties instance. so if it don't work, you can try this xaml

<Border DataContext="{Binding Properties}">
     <TextBlock Text="{Binding Path=ErrorStatus}" /> 
</Border>
0

精彩评论

暂无评论...
验证码 换一张
取 消