我正在创建一个Xamarin。表单应用程序。我无法从代码隐藏设置样式。以下代码位于我的页面的构造函数中。
Style greenButton = new Style(typeof(Button)) { Setters = { new Setter{Property = Button.BackgroundColorProperty, Value = Color.Green }, new Setter{Property = Button.TextColorProperty, Value = Color.Red} } }; Resources = new ResourceDictionary(); Resources.Add(greenButton); Button createSL = new Button(); createSL.Text = "Create Stack Layout"; createSL.Style = (Style) Resources["greenButton"];
上述代码给出了 error message .
“找不到键”异常,表明“绿色按钮”不存在 在字典里。
但我已经做了Xamarin中提到的一切。表单文档。请帮我修一下!
你的 Resources.Add 需要包含样式的基于文本的名称,以便按名称检索:
Resources.Add
即
Resources.Add ("greenButton", greenButton);