代码之家  ›  专栏  ›  技术社区  ›  imthath

在资源字典异常-Xamarin中找不到密钥。形式

  •  2
  • imthath  · 技术社区  · 7 年前

    我正在创建一个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中提到的一切。表单文档。请帮我修一下!

    1 回复  |  直到 7 年前
        1
  •  5
  •   SushiHangover    7 年前

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

    Resources.Add ("greenButton", greenButton);
    
    推荐文章