Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use xaml ApplicationResources with an MvvmCross Application

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

like image 922
User1 Avatar asked Feb 08 '23 22:02

User1


1 Answers

So after @Davids answer I realised that providing a new App class was not going to get anywhere.

I found that I could set a ResourceDictionary in code behind using

Application.Current.ResourceDictionary = new ResourceDictionary();
//add styles here

I tried to then set this to a Xaml ResourceDictionary this in Xaml like so:

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

but I get

MyApp.Styles cannot derive from sealed type Xamarin.Forms.ResourceDictionary

(Thanks Xamarin)

Therefore My Final Solution was this:

Add a ContentPage to My PCL project:

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

Then in MainActivity after the LoadApplication call do:

var styles = new Styles();
Xamarin.Forms.Application.Current.Resources = styles.Resources;
like image 77
User1 Avatar answered Feb 11 '23 11:02

User1