我可以将 xaml ApplicationResources 与 MvvmCross 应用程序一起使用吗
Can I use xaml ApplicationResources with an MvvmCross Application
我正在尝试向使用 MvvmCross
的 Xamarin
应用程序添加一些 ApplicationResources
我目前有一个 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 >
我还在 App.cs
class 中添加了 partial
关键字,但这表示:
Partial declarations of must not specify different base classes
所以我试过了
<?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>
但我得到:
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'.
有没有办法让 Xaml
应用程序资源与 MvvmCross
Xamarin
应用程序
可能不是您要找的答案,但我认为您做不到。事实上,我认为您根本无法将 Xamarin.Forms 应用程序级样式与 MVVMCross Xaml 或其他方式一起使用。这些样式似乎是从应用程序 Class 中实现的。 Application 包含 ResourceDictionary,因此 Application 似乎负责应用 Dictionary 中定义的样式。
MvxApplication 没有这样的 属性 因此,即使您将它们定义为 XAML 将它们加载到 ResourceDictionary 中,我也找不到可以应用它们的地方。
所以在@Davids 回答后我意识到提供一个新的 App
class 不会有任何进展。
我发现我可以在后面的代码中使用
设置一个 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 cannot derive from sealed type
Xamarin.Forms.ResourceDictionary
(感谢 Xamarin)
因此我的最终解决方案是:
将 ContentPage
添加到我的 PCL 项目:
<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
调用 do:
var styles = new Styles();
Xamarin.Forms.Application.Current.Resources = styles.Resources;
我正在尝试向使用 MvvmCross
Xamarin
应用程序添加一些 ApplicationResources
我目前有一个 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 >
我还在 App.cs
class 中添加了 partial
关键字,但这表示:
Partial declarations of must not specify different base classes
所以我试过了
<?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>
但我得到:
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'.
有没有办法让 Xaml
应用程序资源与 MvvmCross
Xamarin
应用程序
可能不是您要找的答案,但我认为您做不到。事实上,我认为您根本无法将 Xamarin.Forms 应用程序级样式与 MVVMCross Xaml 或其他方式一起使用。这些样式似乎是从应用程序 Class 中实现的。 Application 包含 ResourceDictionary,因此 Application 似乎负责应用 Dictionary 中定义的样式。
MvxApplication 没有这样的 属性 因此,即使您将它们定义为 XAML 将它们加载到 ResourceDictionary 中,我也找不到可以应用它们的地方。
所以在@Davids 回答后我意识到提供一个新的 App
class 不会有任何进展。
我发现我可以在后面的代码中使用
设置一个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 cannot derive from sealed type Xamarin.Forms.ResourceDictionary
(感谢 Xamarin)
因此我的最终解决方案是:
将 ContentPage
添加到我的 PCL 项目:
<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
调用 do:
var styles = new Styles();
Xamarin.Forms.Application.Current.Resources = styles.Resources;