简体   繁体   English

如何使用 XAML 文件(Xamarin.Forms,Android)的 Resources 文件夹中的 Styles

[英]How to Use Styles from the Resources Folder in XAML Files (Xamarin.Forms, Android)

In my project (only for Android), in the Resources/values/styles.xml folder, I created a style with the following code:在我的项目中(仅适用于 Android),在 Resources/values/styles.xml 文件夹中,我使用以下代码创建了一个样式:

<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">    
    <item name="android:windowEnableSplitTouch">false</item>
    <item name="android:splitMotionEvents">false</item>
</style>

I would like to know how it can be used in a XAML file outside of the Resources folder.我想知道如何在 Resources 文件夹之外的 XAML 文件中使用它。

I plan to use this style in the biggest Grid of the following code (to disable multi-touch on it):我计划在以下代码的最大网格中使用这种样式(禁用多点触控):

<?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:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
             xmlns:tt="clr-namespace:TouchTracking.Forms;assembly=TouchTracking.Forms"
             x:Class="ToothScan.Views.FingerPaintPage"
             Title="Drawing"
             BackgroundColor="Gainsboro"
             Shell.FlyoutBehavior="Disabled">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

<Picker x:Name="colorPicker"
        Title="Color"
        Grid.Row="0"
        Grid.Column="0"
        Margin="10"
        BackgroundColor="DarkGray"
        FontSize="15"
        HorizontalTextAlignment="Center">
    <Picker.Items>
        <x:String>Red</x:String>
        <x:String>Green</x:String>
        <x:String>Blue</x:String>
        <x:String>Aqua</x:String>
        <x:String>Fuchsia</x:String>
        <x:String>Yellow</x:String>
        <x:String>Black</x:String>
        <x:String>Gray</x:String>
        <x:String>White</x:String>
    </Picker.Items>

    <Picker.SelectedIndex>
        0
    </Picker.SelectedIndex>
</Picker>
<Picker x:Name="widthPicker"
        Title="Width"
        Grid.Row="0"
        Grid.Column="1"
        Margin="10"
        BackgroundColor="DarkGray"
        FontSize="15"
        HorizontalTextAlignment="Center">
    <Picker.Items>
        <x:String>Thin (1 px)</x:String>
        <x:String>Thinish (2 px)</x:String>
        <x:String>Medium (5 px)</x:String>
        <x:String>Thickish (10 px)</x:String>
        <x:String>Thick (20 px)</x:String>
    </Picker.Items>

    <Picker.SelectedIndex>
        0
    </Picker.SelectedIndex>
</Picker>

<Button Text="Clear"
        x:Name="ClearBtn"
        Grid.Row="0"
        Grid.Column="2"
        HorizontalOptions="Center"
        VerticalOptions="Center"
        Clicked="OnClearButtonClicked"
        Margin="10"/>

<Grid Grid.Row="1"
      Grid.Column="0"
      Grid.ColumnSpan="3">

    <skia:SKCanvasView x:Name="canvasView"
                       PaintSurface="OnCanvasViewPaintSurface"/>
    <Grid.Effects>
        <tt:TouchEffect Capture="True"
                        TouchAction="OnTouchEffectAction"/>
    </Grid.Effects>
    </Grid>
</Grid>

If you use the style.xml in the android part, all the pages in the app will disable the multi-touch.如果您在 android 部分中使用 style.xml,则应用程序中的所有页面都将禁用多点触控。

So you can try to use a ContentPage custom renderer or a Grid custom renderer to disable the multi-touch in the page or the grid.因此,您可以尝试使用 ContentPage 自定义渲染器或 Grid 自定义渲染器来禁用页面或网格中的多点触控。 Such as:如:

public class MyPage : PageRenderer
{
    public MyPage(Context context) : base(context) { }
    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
        this.MotionEventSplittingEnabled = false;
        
        base.OnElementChanged(e);
    }
    public override bool DispatchTouchEvent(MotionEvent e)
    {
        return e.PointerCount ==1 && base.DispatchTouchEvent(e);
    }
}


public class MyGrid : ViewRenderer<Grid, Android.Widget.GridView>
{
    public MyGrid(Context context) : base(context) { }
    protected override void OnElementChanged(ElementChangedEventArgs<Grid> e)
    {
        base.OnElementChanged(e);
        Control.MotionEventSplittingEnabled = false;
    }
    public override bool DispatchTouchEvent(MotionEvent e)
    {
        return e.PointerCount == 1 && base.DispatchTouchEvent(e);
    }
}

I publish useful information from myself.我发布自己的有用信息。 Let me tell you right away that I haven't found my variant of using XAML files , I've found the solution for the cases when the drawing error occurs due to smartphone optimization (in my case, MIUI optimization), to solve it you just need to add the following code to MainActivity:我马上告诉你,我没有找到我使用XAML文件的变种,我已经找到了由于智能手机优化(在我的情况下是MIUI优化)导致绘图错误的情况的解决方案,来解决你只需在 MainActivity 中添加以下代码:

public override bool DispatchTouchEvent(MotionEvent e)
{
    try
       {
          return base.DispatchTouchEvent(e);
       }
    catch
       {
          return false;
       }
}

It will allow you to intercept the error and not recognise the touch in cases where it occurs.它将允许您拦截错误并且在发生错误的情况下无法识别触摸。

PS: the error will show up in Visual Studio, but when using the app on the phone everything will work correctly. PS:错误会出现在 Visual Studio 中,但是在手机上使用该应用程序时,一切都会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM