【问题标题】:Key is not found in resource dictionary Exception - Xamarin.Forms在资源字典中找不到键异常 - Xamarin.Forms
【发布时间】:2018-02-06 05:01:35
【问题描述】:

我正在创建一个 Xamarin.Forms 应用程序。我无法从后面的代码中设置样式。以下代码在我页面的构造函数中。

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

A Key not found 异常声明“greenButton”不存在 在字典里。

但我已经完成了 Xamarin.Forms 文档中提到的所有操作。请帮我解决它!

【问题讨论】:

    标签: c# xamarin xamarin.forms resourcedictionary


    【解决方案1】:

    您的Resources.Add 需要包含样式的基于文本的名称,以便按名称检索它:

    Resources.Add ("greenButton", greenButton);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      相关资源
      最近更新 更多