【发布时间】:2022-01-21 18:46:32
【问题描述】:
我有一个 ImageButton 在 Android 中可以正常工作,但在 iOS 中不能正常工作,相同的代码(XAML、代码隐藏和 ViewModel )在 iOS 的另一个视图上工作正常,但不适用于这个。
第二个没有断点,事件后面的代码或 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";
}
}
}
【问题讨论】:
-
也许你可以在 XCode 中打开 Xamarin 为 iOS 构建的内容,看看是否有任何明显的问题?
-
谢谢,我该怎么做?
-
ISavedPlaceRepository、NotWorkingViewModel和NotWorkingViewModel是什么?如果对您方便的话,能否请您将基本演示发布到 github 或 onedriver 以便我们进行测试? -
我想,不幸的是我不能分享任何代码,因为它是公司私有的,但我可以告诉你
ISavedPlaceRepository与工作代码相同,两个视图模型都是几乎与工作代码相同,但问题是,由于按钮根本没有被点击,感觉无论它是什么它都不会抛出错误,但我不知道它是否是相关但工作按钮在实际页面上,而不是弹出窗口上的不工作按钮,但问题是弹出窗口上的每个其他按钮都有效,只有这个 -
如果你添加一个老式的
Debug.WriteLine("HERE")类型的语句,它会打印还是点击的处理程序甚至没有被调用?
标签: c# xamarin.forms