1
문자열이 포함 된 Singleton을 만들었습니다. 이제이 문자열을 Xaml의 TextBlock에 바인딩하고 싶습니다.xaml (WinRT/C#)에서 싱글 톤의 문자열을 바인딩하는 방법
<TextBlock Visibility="Visible" Text="{Binding singleton.Instance.newsString, Mode=TwoWay}"/>
WinRT 앱을 실행할 때 TextBlock-Text-String은 비어 있습니다.
편집 1 :
지금이 실행됩니다. 하지만 싱글 톤의 문자열을 변경하면 TextBlock이 업데이트되지 않습니다. 여기
내가 XAML에서 내가 <TextBlock Visibility="Visible" Text="{StaticResource newsStringResource}" />
과 자원을이
singleton.Instance.newsString = "Breaking news before init";
this.Resources.Add("newsStringResource", singleton.Instance.newsString);
this.InitializeComponent();
singleton.Instance.newsString = "Breaking news AFTER init";
와 결합 않는 XAML 뒤에 내 코드에서 내 싱글
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;
}
}
에서 C# 코드입니다
이 코드를 사용하면 TextBlock에 "초기화 전 뉴스 속보"가 표시됩니다. 지금 뭐야?
귀하의 조언에 감사드립니다! 그러나 이제 또 다른 문제가 나타났습니다. "Edit1"을보십시오 –
당신은 리소스를 바인딩하지 않습니다. 기본적으로 리소스를 설정합니다. 싱글 톤 인스턴스를 리소스로 설정 한 다음 'Text = "{Binding DownloadMgrString, Source = {StaticResource MySingletonResourceKey}" –