这是我在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。
我的步骤:
<ContentPage>
重命名为<ResourceDictionary>
,并设置简单的样式。ResourceDictionary
而不是ContentPage
。我一直得到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中发生了相同的异常。
非常感谢任何帮助!
我尝试过的建议:
还有其他建议吗?