2011-07-28 5 views
0

前回、Windows Phone 7アプリケーションでMVVMのプロパティを使用する方法について質問を提出しました。 私は優れたアドバイスによってうまくできました。私の前の質問を見てください。私のコーディングを通じMVVMのプロパティクラスとメソッドクラスを分割する方法

Can not bind textblock property from another class to UI class using 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"; 
    } 
} 
+0

は、私はあなたが今持っている定型の重い量を避けるために、http://code.google.com/p/notifypropertyweaver/を使用して起動勧告することができます。 –

答えて

1

このXAMLを試すことができます以下はPropertiesのプロパティへのアクセス権を持っているAuthenticationを可能にし、それが仕事ができる方法を示しています。

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

あなたのコメントありがとうございます。しかし、それはうまくいかなかった... –

+0

@okameこれがどのように動作するかを示す答えを更新しました。これがあなたが達成しようとしているものでなければ、あなたは明らかにするために質問を更新してください –

+0

それはクールです!私はあなたの解決策によって私の質問を解決することができました。本当にありがとう!! –

0

は、私はあなたが持っていると仮定同じ古い

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

する必要がありますが、時には、あなたは、プロパティのインスタンスを変更します。 ので、それが動作しない場合、あなたは

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

ありがとう!私はそれを試してみます! –

関連する問題