首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“不知道Xamarin.Forms.Color”

“不知道Xamarin.Forms.Color”
EN

Stack Overflow用户
提问于 2020-02-06 07:01:05
回答 1查看 324关注 0票数 2

为什么给我这个代码“不知道关于Xamarin.Forms.Color”的例外?

例外细节:

System.AggregateException Zpráva=One or more errors occurred. (Don't know about Xamarin.Forms.Color) Zdroj= StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2027 at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2759 at System.Threading.Tasks.Task.Wait () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2625 at Notes.Data.NoteDatabase..ctor (System.String dbPath) [0x00015] in C:\Users\foksak\source\repos\Notes\Notes\Notes\Data\NoteDatabase.cs:15 at Notes.App.get_Database () [0x0000e] in C:\Users\foksak\source\repos\Notes\Notes\Notes\App.xaml.cs:18 at Notes.NotesPage.OnAppearing () [0x0001b] in C:\Users\foksak\source\repos\Notes\Notes\Notes\NotesPage.xaml.cs:19 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.39(intptr,intptr at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.39(intptr,intptr)

注模式:

代码语言:javascript
运行
复制
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#:

代码语言:javascript
运行
复制
[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#中的数据种子:

代码语言:javascript
运行
复制
protected override async void OnAppearing()
{
    base.OnAppearing();

    listView.ItemsSource = await App.Database.GetNotesAsync();
}

Xaml:

代码语言:javascript
运行
复制
    <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-06 09:47:20

在sqlite中不能有一个Color字段,所以忽略它。

代码语言:javascript
运行
复制
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字段绑定到视图之前初始化它,如下所示:

代码语言:javascript
运行
复制
note.Colorrr = note.Colorr.FromHex(x);

然后命名您的框架以更新颜色,并删除绑定:

代码语言:javascript
运行
复制
<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>

并在保存方法中更新它:

代码语言:javascript
运行
复制
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对象。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60089355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档