【问题标题】:Xamarin conditional xaml based on compilation directive基于编译指令的 Xamarin 条件 xaml
【发布时间】:2020-07-02 16:36:25
【问题描述】:

我需要相同的 Does XAML have a conditional compiler directive for debug mode? 但在 Xamarin xaml 中;我已经尝试了建议的解决方案,但出现此错误:

Type mc:Choice not found in xmlns http://schemas.openxmlformats.org/markup-compatibility/2006

我尝试使用 xcc https://github.com/firstfloorsoftware/xcc,但它似乎不适用于 .netstandard 2.0 / Xamarin.Forms 4.5

【问题讨论】:

  • 这是 WPF 的解决方案,而不是 Xamarin
  • 我知道,我想在 xamarin 中实现类似的结果
  • 如果你想在app.xaml中添加全局style,如果是,你可以像这个线程一样添加:docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…
  • 到目前为止,您是否找到了一些东西,(我正在寻找相同的条件构建,例如在调试模式下忽略此 xaml 行),而不是答案中提到的设计时间)

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

它在 Xamarin 的 XAML 中略有不同。 Xamarin 中有设计时命名空间。

您将使用 ma​​rkup-compatibility xml 命名空间并将设计命名空间标记为 Ignorable

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://xamarin.com/schemas/2014/forms/design" 
             xmlns:ref="clr-namespace:XamUtilities.Views;assembly=XamUtilities" 
             mc:Ignorable="d" 
             x:Class="Sample.MainPage">
    <ContentPage.Content>
        <d:Label Text="This will be displayed only in previewer"/>
        <Label Text="This will be displayed in actual app"/>
    </ContentPage.Content>
</ContentPage>

在后面的代码中也可以设置值

[DesignTimeVisible (true)]
public partial class MainPage : ContentPage
{
    public MainPage ()
    {
        InitializeComponent ();
        if (DesignMode.IsDesignModeEnabled) 
        {
            // Previewer only code  
            //add your bindings in here
        }
    }
}

【讨论】:

  • 我知道这一点,但我需要在最终的 apk 中有条件地编译一些东西。
猜你喜欢
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 2010-11-15
  • 1970-01-01
相关资源
最近更新 更多