我可以将xaml ApplicationResources与MvvmCross应用程序一起使用

时间:2015-09-29 09:36:15

标签: c# xaml xamarin mvvmcross xamarin.forms

我正在尝试向使用ApplicationResources

Xamarin应用添加一些MvvmCross

我目前有一个App.cs文件,如下所示:

public class App : MvxApplication
{
    /// <summary>
    /// Initializes this instance.
    /// </summary>
    public override void Initialize()
    {
       this.CreatableTypes()
            .EndingWith("Service")
            .AsInterfaces()
            .RegisterAsLazySingleton();
        .....etc etc..
    }
}

我现在关注Working with Styles using Xaml for your application并且已经:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mvvmcross="clr-namespace:Cirrious.MvvmCross.ViewModels;assembly=Cirrious.MvvmCross"
             x:Class="FieldStrikeMove.Core.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="TestStyle" TargetType="Label">
                <Setter Property="TextColor" Value="Green" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application >

我还在partial课程中添加了App.cs关键字,但这说明了:

  

部分声明不得指定不同的基类

所以我试过

<?xml version="1.0" encoding="utf-8" ?>
<mvvmcross:MvxApplication xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mvvmcross="clr-namespace:Cirrious.MvvmCross.ViewModels;assembly=Cirrious.MvvmCross"
             x:Class="FieldStrikeMove.Core.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="TestStyle" TargetType="Label">
                <Setter Property="TextColor" Value="Green" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</mvvmcross:MvxApplication>

但我明白了:

  

错误35类型&#39; FieldStrikeMove.Core.App&#39;不能用作类型参数&#39; TView&#39;在泛型类型或方法&#39; Xamarin.Forms.Xaml.Extensions.LoadFromXaml(TView,System.Type)&#39;。来自&#39; FieldStrikeMove.Core.App&#39;没有隐式参考转换。到&#39; Xamarin.Forms.BindableObject&#39;。

是否可以通过Xaml MvvmCross应用程序获得Xamarin个应用资源

2 个答案:

答案 0 :(得分:6)

所以在@Davids回答之后,我意识到提供一个新的App课程并不会随处可见。

我发现我可以使用

在代码中设置ResourceDictionary
Application.Current.ResourceDictionary = new ResourceDictionary();
//add styles here

我尝试在Xaml中将其设置为Xaml ResourceDictionary,如下所示:

<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Styles">
  <Style x:Key="labelStyle" TargetType="Label">
    <Setter Property="TextColor" Value="Green" />
  </Style>
</ResourceDictionary>

但我得到

  

MyApp.Styles无法从密封类型派生   Xamarin.Forms.ResourceDictionary

(谢谢Xamarin)

因此,我的最终解决方案是:

向我的PCL项目添加ContentPage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Styles">
  <ContentPage.Resources>
    <ResourceDictionary>
      <Style x:Key="labelStyle" TargetType="Label">
        <Setter Property="TextColor" Value="Green" />
      </Style>
    </ResourceDictionary>
  </ContentPage.Resources>
</ContentPage>

然后在MainActivity来电后LoadApplication执行:

var styles = new Styles();
Xamarin.Forms.Application.Current.Resources = styles.Resources;

答案 1 :(得分:2)

可能不是你想要的答案,但我认为你不能。事实上,我不认为你可以使用MVVMCross Xaml或其他方式使用Xamarin.Forms应用程序级别样式。样式似乎是从Application Class中实现的。应用程序包含一个ResourceDictionary,因此Application应该负责应用Dictionary中定义的样式。

MvxApplication没有这样的属性,因此即使你把它们定义为XAML将它们加载到ResourceDictionary中,我也找不到你应用它们的地方。

相关问题