我有这个:
var MyText = new TextBlock();
MyText.Text = "blah";
MyText.Style = /* ??? */;
在XAML中,我可以像这样设置样式:<TextBlock Text="blah" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
但是我如何在C#中实现呢?
编辑:
Error 1 'Windows.UI.Xaml.Application' does not contain a definition for 'FindResource' and no extension method 'FindResource' accepting a first argument of type 'Windows.UI.Xaml.Application' could be found (are you missing a using directive or an assembly reference?)
Error 1 'Geodropper.HubPage' does not contain a definition for 'FindResource' and no extension method 'FindResource' accepting a first argument of type 'Geodropper.HubPage' could be found (are you missing a using directive or an assembly reference?)
当我尝试使用(Style)this.FindResource("ListViewItemTextBlockStyle");
和(Style)App.Current.FindResource("ListViewItemTextBlockStyle")
时,出现了以下错误。
MyText.Style = (Style)this.FindResource("ListViewItemTextBlockStyle");
这里的this
指的是包含资源的窗口或元素,也可以使用(Style)App.Current.FindResource("ListViewItemTextBlockStyle")
。 - Xi Sigma(Style)App.Current.Resources["ListViewItemTextBlockStyle"]
或者(Style)this.Resources["ListViewItemTextBlockStyle"]
。 - Xi Sigma