简体   繁体   中英

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:

<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.

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.

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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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