繁体   English   中英

Xamarin.Forms ImageButton 不起作用,单击不起作用,调试断点根本不会在 iOS 上受到影响

[英]Xamarin.Forms ImageButton doesn't work, clicking does not work and debug breakpoint doesn't get hit at all on iOS only

I have an ImageButton that works fine in Android but not in iOS, the same exact code ( both XAML, code-behind and ViewModel ) work fine on another view in iOS but not for this one.

第二个没有断点,事件背后的代码或 ViewModel 命令都没有,并且该按钮的背景很宽,我已经更改了颜色以查看它与第一个有效的完全相同。

我很困惑为什么它不起作用。

有效的代码:

Xaml:

<ImageButton Grid.Row="1"
             Grid.RowSpan="4"
             x:Name="SaveIconName"
             BorderColor="Transparent"
             BackgroundColor="Transparent"
             HeightRequest="24"
             WidthRequest="24"
             Aspect="AspectFill"
             HorizontalOptions="End"
             VerticalOptions="Center"
             Command="{Binding Source={RelativeSource AncestorType={x:Type vm:PartnerViewModel}}, Path=SavePlaceTapped}"
                             CommandParameter="{Binding Place.PlaceId}"
                             Clicked="Save_Clicked"/>

代码隐藏:

namespace App.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class WorkingPage : ContentPage
{
    WorkingViewModel workingViewModel;
    public ISavedPlaceRepository savedPlaceRepository =>
        DependencyService.Get<ISavedPlaceRepository>();

    public string SaveIcon { get; set; }

    public WorkingPage()
    {
        InitializeComponent();
        BindingContext = workingViewModel = new WorkingViewModel();
    }
    public WorkingPage(WorkingModel popupModel)
    {
        InitializeComponent();
        BindingContext = workingViewModel = new WorkingViewModel(popupModel);
        SaveIconName.Source = "icon_heart_red_96.png";
    }

    private void Save_Clicked(object sender, EventArgs e)
    {
        if (Device.RuntimePlatform == Device.Android)
        {
            Vibration.Vibrate(TimeSpan.FromMilliseconds(10));
        }
        SaveIconName.Source = "icon_heart_full_red_96.png";
    }
}

}

不起作用的代码:

Xaml:

<ImageButton Grid.RowSpan="3"
             x:Name="SaveIconName"
             BorderColor="Transparent"
             BackgroundColor="Transparent"
             HeightRequest="24"
             WidthRequest="24"
             Aspect="AspectFill"
             HorizontalOptions="End"
             VerticalOptions="Center"
             Command="{Binding Source={RelativeSource AncestorType={x:Type vm:NotPartnerPopupViewModel}}, Path=SavePlaceTapped}"
                                         CommandParameter="{Binding Place.PlaceId}"
                                     Clicked="Save_Clicked"/>

代码隐藏:

namespace App.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NotPartnerPopupPage : Popup
{
    NotWorkingViewModel notWorkingViewModel;
    public ISavedPlaceRepository savedPlaceRepository =>
        DependencyService.Get<ISavedPlaceRepository>();

    public NotWorkingPage()
    {
        InitializeComponent();
        BindingContext = notWorkingViewModel = new NotWorkingViewModel();
    }

    public NotWorkingPage(NotWorkingModel popupModel)
    {
        InitializeComponent();
        DisplayInfo mainDisplayInfo = new DisplayInfo();
        if (Device.RuntimePlatform == Device.iOS)
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
                Size size = new Size();
                var pixelWidth = ConvertToPixels(mainDisplayInfo.Width, mainDisplayInfo.Density);
                size.Width = pixelWidth - 40;
                size.Height = 300;
                this.Size = size;
                InsideFrame.Padding = new Thickness(10);
                InsideFrame.Margin = new Thickness(-54);
            });

        }
        else
        {
            mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
            Size size = new Size();
            var pixelWidth = ConvertToPixels(mainDisplayInfo.Width, mainDisplayInfo.Density);
            size.Width = pixelWidth;
            size.Height = 300;
            this.Size = size;
            InsideFrame.Padding = new Thickness(0);
            InsideFrame.Margin = new Thickness(-10);
        }
        BindingContext = notWorkingViewModel = new NotWorkingViewModel(popupModel);

        SaveIconName.Source = "icon_heart_red_96.png";
    }

    public void Save_Clicked(object sender, EventArgs e)
    {
        Vibration.Vibrate(TimeSpan.FromMilliseconds(10));
        SaveIconName.Source = "icon_heart_full_red_96.png";
    }
}

}

我找到了解决方案,我不知道问题出在哪里,但是我已经将框架的边距更改为扩展程度较低的框架并且它起作用了,一定是人为的框架拉伸弄乱了控件

暂无
暂无

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

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