正如 Eugene 所说,使用 UserInteraction 插件。不幸的是,目前没有 Windows Phone 实现,所以这是我在此期间使用的代码:
public class WindowsPhoneUserInteraction : IUserInteraction
{
public void Confirm(string message, Action okClicked, string title = null, string okButton = "OK", string cancelButton = "Cancel")
{
Confirm(message, confirmed =>
{
if (confirmed)
okClicked();
},
title, okButton, cancelButton);
}
public void Confirm(string message, Action<bool> answer, string title = null, string okButton = "OK", string cancelButton = "Cancel")
{
var mbResult = MessageBox.Show(message, title, MessageBoxButton.OKCancel);
if (answer != null)
answer(mbResult == MessageBoxResult.OK);
}
public Task<bool> ConfirmAsync(string message, string title = "", string okButton = "OK", string cancelButton = "Cancel")
{
var tcs = new TaskCompletionSource<bool>();
Confirm(message, tcs.SetResult, title, okButton, cancelButton);
return tcs.Task;
}
public void Alert(string message, Action done = null, string title = "", string okButton = "OK")
{
MessageBox.Show(message, title, MessageBoxButton.OK);
if (done != null)
done();
}
public Task AlertAsync(string message, string title = "", string okButton = "OK")
{
var tcs = new TaskCompletionSource<object>();
Alert(message, () => tcs.SetResult(null), title, okButton);
return tcs.Task;
}
public void Input(string message, Action<string> okClicked, string placeholder = null, string title = null, string okButton = "OK", string cancelButton = "Cancel", string initialText = null)
{
throw new NotImplementedException();
}
public void Input(string message, Action<bool, string> answer, string placeholder = null, string title = null, string okButton = "OK", string cancelButton = "Cancel", string initialText = null)
{
throw new NotImplementedException();
}
public Task<InputResponse> InputAsync(string message, string placeholder = null, string title = null, string okButton = "OK", string cancelButton = "Cancel", string initialText = null)
{
throw new NotImplementedException();
}
public void ConfirmThreeButtons(string message, Action<ConfirmThreeButtonsResponse> answer, string title = null, string positive = "Yes", string negative = "No", string neutral = "Maybe")
{
throw new NotImplementedException();
}
public Task<ConfirmThreeButtonsResponse> ConfirmThreeButtonsAsync(string message, string title = null, string positive = "Yes", string negative = "No", string neutral = "Maybe")
{
throw new NotImplementedException();
}
}
您会注意到并非所有内容都已实现,即使是那些有限的位(例如,您无法设置 OK 广告 Cancel 按钮文本)
当然,我还需要在 setup.cs 中注册它:
Mvx.RegisterSingleton<IUserInteraction>(new WindowsPhoneUserInteraction());