gh0*_*0de 3 c# xaml xamarin xamarin.forms
我正在寻找一种在 Xamarin.Forms 中在运行时动态加载 XAML 文件的解决方案,以便它可以显示为视图/内容页。
我已经找到了这个问题的一些答案,但它们不再起作用或样本被删除:
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>
Run Code Online (Sandbox Code Playgroud)
您应该能够使用LoadFromXaml 扩展方法。
添加此使用:
using Xamarin.Forms.Xaml;
然后像这样膨胀你的页面:
// TODO your XAML here
var xaml = "<ContentPage></ContentPage>";
var loadedXamlPage = new ContentPage();
loadedXamlPage.LoadFromXaml(xaml);
Run Code Online (Sandbox Code Playgroud)
可以在此处找到示例存储库。