为什么给我这个代码“不知道 Xamarin.Forms.Color”异常?
异常详细信息:
System.AggregateException Zpráva=发生一个或多个错误。 (不知道 Xamarin.Forms.Color)Zdroj= StackTrace:位于/Users/builder/jenkins/workspace/archive-mono/2019 中的 System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] -08/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2027 在 System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System. Threading.CancellationToken取消 token )[0x00043]在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2759 位于/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corert/src/System 中的 System.Threading.Tasks.Task.Wait () [0x00000]。 Private.CoreLib/src/System/Threading/Tasks/Task.cs:2625 位于 C:\Users\foksak\source\repos\Notes\Notes 中的 Notes.Data.NoteDatabase..ctor (System.String dbPath) [0x00015]\Notes\Data\NoteDatabase.cs:15 在 Notes.App.get_Database () [0x0000e] 在 C:\Users\foksak\source\repos\Notes\Notes\Notes\App.xaml.cs:18 在 Notes.NotesPage .OnAppearing () [0x0001b] 在 C:\Users\foksak\source\repos\Notes\Notes\Notes\NotesPage.xaml.cs:19 处(包装动态方法)Android.Runtime.DynamicMethodNameCounter.39(intptr,intptr
在(包装 native 到托管)Android.Runtime.DynamicMethodNameCounter.39(intptr,intptr)
备注型号:
namespace Notes.Models
{
public class Note
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Text { get; set; }
public string Title { get; set; }
public string Picture { get; set; }
public string Colorr { get; set; }
public Color Colorrr { get; set; }
}
}
数据输入页面的c#:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NoteEntryPage : ContentPage
{
Dictionary<string, string> dic = new Dictionary<string, string>() {
{"Color1","#d8daea"},
{"Color2", "#ecd67b"},
{"Color3", "#3f6018"},
{"Color4", "#ff8847" }
};
public NoteEntryPage()
{
InitializeComponent();
}
async void OnSaveButtonClicked(object sender, EventArgs e)
{
var note = (Note)BindingContext;
string x = dic[note.Colorr];
note.Colorrr = Color.FromHex(x);
await App.Database.SaveNoteAsync(note);
await Navigation.PopAsync();
}
async void OnDeleteButtonClicked(object sender, EventArgs e)
{
var note = (Note)BindingContext;
await App.Database.DeleteNoteAsync(note);
await Navigation.PopAsync();
}
}
输入有效。 当我手动编写 "#ff8847"
而不是 x
时,问题也出现了。
数据在页面中显示,代码如下。 C# 中的数据种子:
protected override async void OnAppearing()
{
base.OnAppearing();
listView.ItemsSource = await App.Database.GetNotesAsync();
}
Xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Notes.NotesPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="+"
Clicked="OnNoteAddedClicked" />
</ContentPage.ToolbarItems>
<ListView x:Name="listView"
Margin="20" RowHeight="80"
ItemSelected="OnListViewItemSelected">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell>
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="Fill">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions >
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackLayout Orientation="Horizontal" Grid.Column="1" Grid.Row="0">
<Label Text="{Binding Title}" FontSize="22" FontAttributes="Bold" />
</StackLayout>
<Label Text="{Binding Text}" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2"/>
<Frame CornerRadius="5" HasShadow="true" Grid.RowSpan="2" BackgroundColor="{Binding Colorrr}" Margin="7" WidthRequest="35">
<Image Source="{Binding Picture}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="30"/>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
最佳答案
您的 sqlite 数据库中不能有 Color
字段,因此请忽略它。
namespace Notes.Models
{
public class Note
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Text { get; set; }
public string Title { get; set; }
public string Picture { get; set; }
public string Colorr { get; set; }
[Ignore]
public Color Colorrr { get; set; }
}
}
在将 Colorrr
字段绑定(bind)到您的 View 之前,您还必须初始化它,如下所示:
note.Colorrr = note.Colorr.FromHex(x);
然后命名框架以更新颜色,并删除绑定(bind):
<Frame x:Name="ColorFrame" CornerRadius="5" HasShadow="true" Grid.RowSpan="2" Margin="7" WidthRequest="35">
<Image Source="{Binding Picture}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="30"/>
</Frame>
并在您的保存方法中更新它:
async void OnSaveButtonClicked(object sender, EventArgs e)
{
var note = (Note)BindingContext;
string x = dic[note.Colorr];
ColorFrame.BackgroundColor = Color.FromHex(x);
await App.Database.SaveNoteAsync(note);
await Navigation.PopAsync();
}
然后它应该按预期工作。
另一个更简洁的路径:使用 MVVM 模式并创建一个 NoteViewModel
实现 INotifyPropertyChanged
来包装您的 Note
对象。
关于c# - "Don' 不了解 Xamarin.Forms.Color”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60089355/