我正在寻找一种在Xamarin.Forms中在运行时动态加载XAML文件的解决方案,以便随后将其显示为View / ContentPage.
我已经找到了解决此问题的方法,但是它们不再起作用或删除了示例:
Xamarin forms.Dynamically load a page
https://forums.xamarin.com/discussion/87727/load-xaml-dynamically-at-runtime
要加载的XAML示例:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Examples.Views.TestView">
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="Label">
<Setter Property="FontSize" Value="Medium" />
<Setter Property="TextColor" Value="Black"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Label Text="Hello World!"></Label>
</StackLayout>
</ContentPage.Content>
</ContentPage>
解决方法:
您应该能够使用LoadFromXaml extension method.
使用以下方法添加:
使用Xamarin.Forms.Xaml;
然后像这样膨胀页面:
// TODO your XAML here
var xaml = "<ContentPage></ContentPage>";
var loadedXamlPage = new ContentPage();
loadedXamlPage.LoadFromXaml(xaml);
可以找到样本样本here.