2

Why gives me this code "Don't know about Xamarin.Forms.Color" exception?

Exception detail:

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)

Note model:

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# of data input page:

[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();
    }
}

Input works. The problem was also in the case when i manualy wrote "#ff8847" instead of x.

Data is desplayer in page with code below. Data seed in 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>
6
  • where Colorr is defined ? Does the class holding Colorr is bound to your view?
    – Roubachof
    Commented Feb 6, 2020 at 8:42
  • @Roubachof there is no mention of Colorr in my code other than the ones which are now published.
    – FoksaK
    Commented Feb 6, 2020 at 8:58
  • We would need all the view model code, the view code and how it's bound to the view. We have too little information, it's more like a riddle for now.
    – Roubachof
    Commented Feb 6, 2020 at 9:05
  • @Roubachof I hope you I posted everything you need. If you need more, write another comment please.
    – FoksaK
    Commented Feb 6, 2020 at 9:30
  • Can you add exception stack trace and message
    – FreakyAli
    Commented Feb 6, 2020 at 9:47

1 Answer 1

4

You can't have a Color field in your sqlite db, so ignore it.

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; }
    }
}

You also have to initialize your Colorrr field before binding it to your view like this:

note.Colorrr = note.Colorr.FromHex(x);

Then name your frame to update the color, and remove the binding:

<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>

And update it in your save method:

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();
}

Then it should work as expected.

Another cleaner path: use the MVVM pattern and create a NoteViewModel implementing INotifyPropertyChanged that will wrap your Note object.

1
  • Since your Note is not implementing INotifyPropertyChanged your should access the frame directly in your code behind, see my edited answer
    – Roubachof
    Commented Feb 6, 2020 at 10:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.