我有一個我撰寫的應用程式,它在 Android 平臺上運行良好。我目前正在將它移植到 iOS,但 iOS 中似乎缺少一個基本功能,或者我不知道如何使其作業。我需要能夠不時彈出一個訊息框。我為 Android 使用以下代碼。
我的視圖模型如下:
public class MessageDialogViewModel : MvxViewModel<string, MessageDialogViewModel.Result>
{
public class Result
{
public Result(bool parm)
{
Success = parm;
}
public bool Success { get; set; }
}
private readonly IMvxNavigationService _navigationService;
public MessageDialogViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
CloseCommand = new MvxAsyncCommand(async () => await _navigationService.Close(this, new Result(true)));
}
public override System.Threading.Tasks.Task Initialize()
{
return base.Initialize();
}
public override void Prepare(string parm)
{
base.Prepare();
Message = parm;
}
public IMvxAsyncCommand CloseCommand { get; private set; }
private string _Message;
public string Message
{
get
{
return _Message;
}
set
{
_Message = value;
RaisePropertyChanged(() => Message);
}
}
}
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@drawable/borderdoublewidth">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TableHeaderTextView"
local:MvxBind="Text Message" />
<LinearLayout
android:orientation="horizontal"
android:gravity="right"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLOSE"
style="@style/DialogButton"
local:MvxBind="Click CloseCommand" />
</LinearLayout>
</LinearLayout>
Android 的視圖是這樣的:
[MvxDialogFragmentPresentation]
[Register(nameof(MessageDialogView))]
public class MessageDialogView : MvxDialogFragment<MessageDialogViewModel>
{
public MessageDialogView()
{
}
protected MessageDialogView(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignore = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.MessageDialogView, null);
return view;
}
}
這是我所有 Android 應用程式中的 goto 彈出功能。這很簡單。我創建了一個 iOS 版本的 View,如下所示:
[MvxModalPresentation(WrapInNavigationController = false,
ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve,
ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen)]
class MessageDialogView : MvxViewController<MessageDialogViewModel>
{
public override void ViewDidLoad()
{
View.BackgroundColor = UIColor.White;
base.ViewDidLoad();
var formView = new UIView();
formView.BackgroundColor = UIColor.Black;
var message = new UILabel() { Text = ViewModel.Message };
formView.Add(message);
message.TextColor = UIColor.White;
message.Font = message.Font.WithSize(20f);
var btnClose = new UIButton(UIButtonType.RoundedRect);
btnClose.SetTitle("Close", UIControlState.Normal);
btnClose.Font = btnClose.Font.WithSize(20f);
btnClose.SetTitleColor(UIColor.Clear.FromHex(0x00D0F7), UIControlState.Normal);
formView.Add(btnClose);
var set = this.CreateBindingSet<MessageDialogView, MessageDialogViewModel>();
set.Bind(btnClose).To(vm => vm.CloseCommand);
set.Apply();
View.Add(formView);
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
formView.AtLeftOf(View),
formView.AtRightOf(View),
formView.AtTopOfSafeArea(View),
formView.AtBottomOf(View)
);
formView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
formView.AddConstraints(
message.AtTopOf(formView, 10f),
message.WithSameCenterX(formView),
btnClose.Below(message, 20f),
btnClose.AtRightOf(formView)
);
}
}
This works, but it's not really a popup. It covers the whole screen. I have tried the other options for ModalPresentationStyle, but the dialog acts the same way no matter what. The docs say that the other options only work on iPads and I am developing for an iPhone. I have seen other apps that have a small popup dialog box. I looked at the code here https://prin53.medium.com/pop-up-xamarin-e2d815441a54 and the example works for his particular purpose, but when I tried to make it work with mine, I get an "Assertion" error that is pretty vague for my level. It seems there has to be a way to popup a small dialog without having to jump through so many hoops. Can anyone help?
Thanks, Jim
uj5u.com熱心網友回復:
好的,這就是我所做的。它有點臟,但它有效。請不要笑。我創建了一個背景清晰的視圖,并從其中彈出了一個標準警報。我能夠使用 Message 屬性并執行 CloseCommand,因此 MessageDialogViewModel 看到它與在 Android 中一樣。這里是:
[MvxModalPresentation(WrapInNavigationController = false,
ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve,
ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen)]
class MessageDialogView : MvxViewController<MessageDialogViewModel>
{
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
View.BackgroundColor = UIColor.Clear;
var okAlertController = UIAlertController.Create("", ViewModel.Message, UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("Close", UIAlertActionStyle.Default, (handler) => ViewModel.CloseCommand.ExecuteAsync()));
PresentViewController(okAlertController, true, null);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364247.html
標籤:ios xamarin xamarin.ios mvvmcross
