我有以下示例应用程序代码:
App.cs(入口点)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace App1
{
public class ListItem
{
public string ItemText { get; set; }
public string ItemDetail { get; set; }
public string Password { get; set; }
}
public class App : Application
{
public App()
{
// The root page of your application
var listView = new ListView
{
ItemTemplate = new DataTemplate(() =>
{
var textCell = new TextCell();
textCell.SetBinding(TextCell.TextProperty, "ItemText");
textCell.SetBinding(TextCell.DetailProperty, "ItemDetail");
return textCell;
}),
VerticalOptions = LayoutOptions.FillAndExpand,
ItemsSource = new List<ListItem>
{
new ListItem
{
ItemText = "Item 1",
ItemDetail = "Detail 1",
Password = "123456"
},
new ListItem
{
ItemText = "Item 2",
ItemDetail = "Detail 2",
Password = "76432"
},
new ListItem
{
ItemText = "Item 3",
ItemDetail = "Detail 3",
Password = "66543"
},
}
};
listView.ItemTapped += ListView_ItemTapped;
var content = new ContentPage
{
Title = "App1",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children =
{
new ScrollView
{
Content = new StackLayout
{
Padding = 10,
Children =
{
listView
}
}
}
}
}
};
MainPage = new NavigationPage(content);
}
private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
await this.MainPage.Navigation.PushAsync(new PasswordPage((e.Item as ListItem).Password));
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
密码页面.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;
namespace App1
{
public class PasswordPage : ContentPage
{
private string Password;
private Entry PasswordEntry = new Entry();
public PasswordPage(string password)
{
this.Password = password;
PasswordEntry.IsPassword = true;
PasswordEntry.Text = password;
var button = new Button();
button.Text = "Show/Hide";
button.Clicked += Button_Clicked;
Content = new StackLayout
{
Children = {
new Label { Text = "Password" },
PasswordEntry,
button
}
};
}
private void Button_Clicked(object sender, EventArgs e)
{
PasswordEntry.IsPassword = !PasswordEntry.IsPassword;
}
}
}
我在以下场景中收到以下错误,仅在 UWP 上(您可以 download 附加的示例项目,运行 UWP 项目并按照指南进行重现):
- 在应用程序主页面上,单击 ListView 的任意项目。
- 在打开的页面上,点击“显示/隐藏”按钮两次。
- 返回上一页。
- 再次单击 ListView 的任意项目。
此错误仅在 UWP/Windows 上发生,并且仅当您切换“条目” View 的“IsPassword”属性时才会发生。如果单击“显示/隐藏”一次,则不会出现错误。
错误:
System.ArgumentException
The parameter is incorrect.
E_RUNTIME_SETVALUE
at Windows.UI.Xaml.DependencyObject.SetValue(DependencyProperty dp, Object value) at Xamarin.Forms.Platform.UWP.EntryRenderer.UpdateIsPassword() at Xamarin.Forms.Platform.UWP.EntryRenderer.OnElementChanged(ElementChangedEventArgs'1 e) at Xamarin.Forms.Platform.UWP.VisualElementRenderer'2.SetElement(VisualElement element) at Xamarin.Forms.Platform.UWP.Platform.CreateRenderer(VisualElement element) at Xamarin.Forms.Platform.UWP.VisualElementPackager.OnChildAdded(Object sender, ElementEventArgs e) at Xamarin.Forms.Platform.UWP.VisualElementPackager.Load() at Xamarin.Forms.Platform.UWP.VisualElementRenderer'2.SetElement(VisualElement element) at Xamarin.Forms.Platform.UWP.Platform.CreateRenderer(VisualElement element) at Xamarin.Forms.Platform.UWP.VisualElementPackager.OnChildAdded(Object sender, ElementEventArgs e) at Xamarin.Forms.Platform.UWP.VisualElementPackager.Load() at Xamarin.Forms.Platform.UWP.VisualElementRenderer'2.SetElement(VisualElement element) at Xamarin.Forms.Platform.UWP.Platform.CreateRenderer(VisualElement element) at Xamarin.Forms.Platform.UWP.VisualElementExtensions.GetOrCreateRenderer(VisualElement self) at Xamarin.Forms.Platform.UWP.NavigationPageRenderer.SetPage(Page page, Boolean isAnimated, Boolean isPopping) at Xamarin.Forms.Platform.UWP.NavigationPageRenderer.OnPushRequested(Object sender, NavigationRequestedEventArgs e) at Xamarin.Forms.NavigationPage.d__90.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Xamarin.Forms.NavigationPage.d__48.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at App1.App.d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.b__6_0(Object state) at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
最佳答案
看起来这实际上是 Forms 中的一个错误。我已经提交了一份错误报告 here .
该问题似乎与设置 native TextBox 控件的 InputScope 属性有关。我创建了一个pull request寻求修复,希望尽快合并并发布。
关于c# - Xamarin 表单 - 切换条目 View 的 IsPassword 时出错(仅在 uwp 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39632530/