How can I use ApplicationResources with an MvvmCross Xamarin Forms Application

JKayJKay USMember ✭✭✭
edited September 2015 in Xamarin.Forms

I am trying to add some ApplicationResources to my Xamarin application that uses MvvmCross

I currently have an App.cs file like so:

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

I am now following Working with Styles using Xaml for your application and have got:

<?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 >

I also added the partial keyword to my App.cs class, but this says:

Partial declarations of must not specify different base classes

So i tried

<?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>

but I get:

Error 35 The type 'FieldStrikeMove.Core.App' cannot be used as type parameter 'TView' in the generic type or method 'Xamarin.Forms.Xaml.Extensions.LoadFromXaml(TView, System.Type)'. There is no implicit reference conversion from 'FieldStrikeMove.Core.App' to 'Xamarin.Forms.BindableObject'.
Is there a way to have Xaml application resources with a MvvmCross Xamarin application

Best Answer

Answers

  • JKayJKay USMember ✭✭✭

    @DavidStrickland. Thanks for the answer, makes sense.

    I agree, MvxApplication not inheriting from Application does seem a rather strange decision. I presumed it would. Kind of annoying.

    The only other way I can think to do it is have a static resource dictionary that every page inherits.

Sign In or Register to comment.