Xamarin表单合并字典抛出ArgumentException

时间:2018-05-01 16:52:05

标签: c# xamarin xamarin.forms

这是我在stackoverflow.com上的第一个问题。

我现在已经尝试了几个小时来创建一个Xamarin Forms中的合并字典(虽然这是第一次)。 我正在关注以下教程/文档:https://jmillerdev.net/creating-a-xamarin-forms-theme-assembly/https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries

我的步骤:

  1. 创建名为“TestApp2”的新应用程序“Mobile App(Xamarin.Forms)”。
  2. 创建新的ContentPage XAML页面(MyDic.xaml),将<ContentPage>重命名为<ResourceDictionary>,并设置简单的样式。
  3. 更改了MyDic.xaml.cs,因此文件继承自ResourceDictionary而不是ContentPage
  4. 向App.xaml添加了代码以合并资源字典。
  5. 我一直得到ArgumentException“类型为'NInterpret.InterpretedObject'的对象'无法转换为'Xamarin.Forms.ResourceDictionary'类型。”当它运行“InitializeComponent();”在App.xaml.cs。

    我在Xamarin Live Player中运行应用程序并使用Visual Studio 2017社区(一切都已更新)。 NuGet显示有五个Xamarin更新,但要应用它们,我将不得不将目标版本从Android 7.1更改为8.1。我没有看到任何信息表明MergedDictionaries只能在Android 8.1中使用。

    App.xaml代码:

    <?xml version="1.0" encoding="utf-8" ?>
    <Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="TestApp2.App"
                 xmlns:style="clr-namespace:TestApp2">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <style:MyDic />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    MyDic.xaml代码:

    <?xml version="1.0" encoding="utf-8" ?>
    <ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="TestApp2.MyDic">
        <Style TargetType="Button">
            <Setter Property="BackgroundColor" Value="Yellow" />
        </Style>
    </ResourceDictionary>
    

    MyDic.xaml.cs代码:

    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace TestApp2
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class MyDic : ResourceDictionary
        {
        }
    }
    

    我尝试使用MergedWith代替以下代码

    <?xml version="1.0" encoding="utf-8" ?>
    <Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="TestApp2.App"
                 xmlns:style="clr-namespace:TestApp2">
        <Application.Resources>
            <ResourceDictionary x:Key="Dictionary1" MergedWith="style:MyDic"/>
        </Application.Resources>
    </Application>
    

    但也会得到一个ArgumentException:“MergedWith应该继承自ResourceDictionary”。

    经过一些测试后,我意识到当我在Visual Studio中添加新的ContentPage时,无法打开页面。我认为这意味着ResourceDictionary因此可能会以某种方式受损。我发现了一个与另一个问题相关的错误报告(https://developercommunity.visualstudio.com/content/problem/160939/bug-adding-xaml-content-page-to-shared-project-xam.html),但试了一下它解决了问题,因此可以打开contentpages而不是ResourceDictionary问题。

    我尝试将ResourceDictionary移动到MainPage.xaml文件,但是在MainPage.xaml.cs中发生了相同的异常。

    非常感谢任何帮助!

    我尝试过的建议:

    • 根据迭戈的建议,我删除了obj和bin文件夹,关闭并重新打开VS,清理并重建(并用手指交叉跳了3次)但没有运气。同样的错误。感谢Diego的建议。 :)
    • 完全重新安装PC没有做任何事情 - 仍然是相同的例外。

    还有其他建议吗?

0 个答案:

没有答案