我的Xamarin.Forms项目有什么问题?

时间:2015-06-23 22:46:48

标签: c# xamarin xamarin.forms

我正在尝试创建一个Xamarin.Forms项目,我有一个BoxView,一个Entry字段和一个Button。我想在我的输入字段中输入颜色的名称,按下按钮,然后让我的BoxView更改为我输入的颜色。这是我到目前为止编写的代码:

查看/ MainView.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="TestGround.MainView">
        <ContentPage.Content>
            <StackLayout VerticalOptions="Center">
                <Label
                    Text="Enter a color:"
                    VerticalOptions="Center"
                    HorizontalOptions="Center"
                />
                <BoxView 
                    Color="{Binding Color}"
                />
                <Entry
                    Text="{Binding Name}"
                />
                <Button
                    Text="Enter"
                    Command="{Binding SetColorCommand}"
                />
            </StackLayout>
        </ContentPage.Content>
</ContentPage>

的ViewModels / MainViewModel.cs

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;


namespace TestGround
{
    public class MainViewModel :INotifyPropertyChanged
    {
        private string _color; //backing field for Greeting

        public string Color //implementation for Greeting method
        { 
            get { return _color; } 

            set 
            { 
                _color = value;
                OnPropertyChanged ("Color"); //Notify view that change has taken place

            }
        }


        public string Name { get; set; } //Name method for Entry field

        public ICommand SetColorCommand { get; set; } //ICommand binds to buttons in XAML

        public void SetColor() //Need a regular method to add to ICommand
        {
            Color = Name;
        }


        //Main VIEW MODEL
        public MainViewModel ()
        {
            //Color = Name;
            Name = "Enter color here";

            SetColorCommand = new Command(SetColor); //Regular command added to ICommand
        }



        #region PropertyChangedRegion

        public void OnPropertyChanged (string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}

这是我得到的错误:

Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException

我想知道我的方法是否错误,我该如何解决它并制作这个非常简单的程序。

2 个答案:

答案 0 :(得分:2)

根据BoxView Documentation,属性“颜色”实际上必须是一种颜色......在那里你将它定义为一个名为颜色的字符串。你的类型混淆了。它应该是 Colors.Blue

答案 1 :(得分:0)

您可以使用类ColorTypeConverter将字符串更改为Color。 我已将您的问题简化为此源代码

//You simplified model
    public class bModel : BindableObject
    {
        private Color _realColor;
        public  Color Color 
        { 
            get { return _realColor; } 

            set 
            {   
                _realColor = value;
                OnPropertyChanged ("Color"); 

            }
        }

        public string _stringColor;
        public string StringColor {
            get {
                return _stringColor;
            }
            set {
                _stringColor = value;
                Color = (Color)(new ColorTypeConverter ()).ConvertFrom (_stringColor);
            }
        }

        public bModel ()
        {
            StringColor = "Blue";
        }
    }
}

//Your simplified page xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="s2c.MyPage">
    <ContentPage.Content>
        <BoxView x:Name="box" Color="{Binding Color}"/>
    </ContentPage.Content>
</ContentPage>

//Your simplified page csharp
public partial class MyPage : ContentPage
{
    public MyPage ()
    {
        InitializeComponent ();
        this.BindingContext = new bModel ();
    }
}