文字列を含むシングルトンを作成しました。今、私はこの文字列を Xaml の TextBlock にバインドしたいと考えています。
<TextBlock Visibility="Visible" Text="{Binding singleton.Instance.newsString, Mode=TwoWay}"/>
WinRT アプリを実行すると、TextBlock-Text-String が空になります。
編集1:
今、それは実行されます。しかし、シングルトンの文字列を変更すると、TextBlock は更新されません。
ここに私のシングルトンからのC#コードがあります
namespace MyApp
{
public sealed class singleton : INotifyPropertyChanged
{
private static readonly singleton instance = new singleton();
public static singleton Instance
{
get
{
return instance;
}
}
private singleton() { }
private string _newsString;
public string newsString
{
get
{
if (_newsString == null)
_newsString = "";
return _newsString;
}
set
{
if (_newsString != value)
{
_newsString = value;
this.RaiseNotifyPropertyChanged("newsString");
}
}
}
private void RaiseNotifyPropertyChanged(string property)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(property));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
xamlの背後にある私のコードでは、これを行います
singleton.Instance.newsString = "Breaking news before init";
this.Resources.Add("newsStringResource", singleton.Instance.newsString);
this.InitializeComponent();
singleton.Instance.newsString = "Breaking news AFTER init";
そしてxamlで私はリソースをバインドします
<TextBlock Visibility="Visible" Text="{StaticResource newsStringResource}" />
このコードでは、TextBlock に "Breaking news before init" と表示されます。今何が悪いの?