如何在xaml(WinRT/C#)中绑定singleton中的字符串
我创建了一个包含字符串的单例。 现在我想把这个字符串绑定到一个文本块和一个Xaml如何在xaml(WinRT/C#)中绑定singleton中的字符串,xaml,binding,windows-runtime,Xaml,Binding,Windows Runtime,我创建了一个包含字符串的单例。 现在我想把这个字符串绑定到一个文本块和一个Xaml <TextBlock Visibility="Visible" Text="{Binding singleton.Instance.newsString, Mode=TwoWay}"/> 在xaml背后的代码中,我这样做 singleton.Instance.newsString = "Breaking news before init"; this.Resource
<TextBlock Visibility="Visible" Text="{Binding singleton.Instance.newsString, Mode=TwoWay}"/>
在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}" />
使用此代码,文本块显示“init之前的突发新闻”。
现在有什么问题?在构建
文本块之前,使用代码隐藏将您的单例添加到应用程序资源中,并按键引用该单例。谢谢您的建议!但现在又出现了另一个问题。看看“Edit1”,你没有绑定一个资源,你基本上设置了一个资源。您需要将单例实例设置为资源,然后执行Text=“{Binding DownloadMgrString,Source={StaticResource MySingletonResourceKey}”
<TextBlock Visibility="Visible" Text="{StaticResource newsStringResource}" />