Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 条目上的多个行为(Xamarin表单)_C#_Xamarin - Fatal编程技术网

C# 条目上的多个行为(Xamarin表单)

C# 条目上的多个行为(Xamarin表单),c#,xamarin,C#,Xamarin,我在一个条目上实现多个行为时遇到了很多麻烦 如果用户输入的类型介于整数、十进制数、日期和文本之间,我想实现用户输入验证 希望您能提供帮助或发送示例您很幸运,因为就在最近,我实现了非常类似的事情: private FormQuestion formModel {get;set;} public Form() { InitializeComponent(); //Hook up events, set layout of main Stack

我在一个条目上实现多个行为时遇到了很多麻烦

如果用户输入的类型介于整数、十进制数、日期和文本之间,我想实现用户输入验证


希望您能提供帮助或发送示例

您很幸运,因为就在最近,我实现了非常类似的事情:

 private FormQuestion formModel {get;set;}
 public Form()
    {
        InitializeComponent();
        //Hook up events, set layout of main Stack
        CreateViews(formModel);
        mainStack.Orientation = StackOrientation.Vertical;
        mainStack.VerticalOptions = LayoutOptions.FillAndExpand;
        mainStack.HorizontalOptions = LayoutOptions.FillAndExpand;
    }
    #endregion
    /// <summary>
    /// Method that returns Stack layout that contains control required for filling up the form if QuestionType is Text.
    /// This method creates StackLayout, Label and Entry then returns it.
    /// </summary>
    /// <param name="formQuestions"></param>
    /// <returns></returns>
    private StackLayout CreateQuestionStackText(FormQuestion formQuestions)
    {
        StackLayout stack = new StackLayout
        {
            Orientation = StackOrientation.Horizontal,
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        Label label = new Label
        {
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.CenterAndExpand
        };
        label.SetBinding(Label.TextProperty, "Text");
        Entry entry = new Entry
        {
            HorizontalOptions = LayoutOptions.EndAndExpand,
            VerticalOptions = LayoutOptions.CenterAndExpand,
            WidthRequest = 200
        };
        entry.SetBinding(Entry.TextProperty, "Value");
        stack.Children.Add(label);
        stack.Children.Add(entry);
        return stack;

    }
    /// <summary>
    /// Method that returns Stack layout that contains control required for filling up the form if QuestionType is Number. 
    /// This method creates StackLayout, Label and Entry that enforces Numerical Keyboard then returns it.  
    /// </summary>
    /// <param name="formQuestions"></param>
    /// <returns></returns>
    private StackLayout CreateQuestionStackNumber(FormQuestion formQuestions)
    {
        StackLayout stack = new StackLayout
        {
            Orientation = StackOrientation.Horizontal,
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        Label label = new Label
        {
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.CenterAndExpand
        };
        label.SetBinding(Label.TextProperty, "Text");
        Entry entry = new Entry
        {
            HorizontalOptions = LayoutOptions.EndAndExpand,
            VerticalOptions = LayoutOptions.CenterAndExpand,
            Keyboard = Keyboard.Numeric,
            WidthRequest = 200
        };
        entry.SetBinding(Entry.TextProperty, "Value");
        stack.Children.Add(label);
        stack.Children.Add(entry);
        return stack;
    }
    /// <summary>
    /// Method that returns Stack layout that contains control required for filling up the form if QuestionType is DropDown
    /// This method creates StackLayout, Label and BindablePicker, which is custom Picker I've created to be able to get work with MVVM, then returns it.
    /// </summary>
    /// <param name="formQuestions"></param>
    /// <returns></returns>
    private StackLayout CreateQuestionStackDropDown(FormQuestion formQuestions)
    {
        StackLayout stack = new StackLayout
        {
            Orientation = StackOrientation.Horizontal,
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        Label label = new Label 
        {
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.CenterAndExpand
        };
        label.SetBinding(Label.TextProperty, "Text");
        BindablePicker picker = new BindablePicker();
        picker.HorizontalOptions = LayoutOptions.EndAndExpand;
        picker.BindingContext = this;
        var bind = this.BindingContext;
        var strings = formQuestions.DataSource.Values;
        var listOfStrings = strings.Split(';');
        List<string> result = new List<string>();
        foreach (var item in listOfStrings)
        {
            result.Add(item);
        }
        picker.ItemsSource = result;
        picker.SelectedIndexChanged += Picker_SelectedIndexChanged;
        //picker.SetBinding(BindablePicker.DisplayMemberPathProperty, new Binding( "Values", BindingMode.TwoWay, new ValuesToListOfStringsConverter(), null));
        picker.SetBinding(BindablePicker.SelectedItemProperty, "Value");
        picker.WidthRequest = 200;
        stack.Children.Add(label);
        stack.Children.Add(picker);
        return stack;
    }
    /// <summary>
    /// Method that returns Stack layout that contains control required for filling up the form if QuestionType is Data
    /// This method creates StackLayout, Label and DataPicker and returns it.
    /// </summary>
    /// <param name="formQuestions"></param>
    /// <returns></returns>
    private StackLayout CreateQuestionStackDate(FormQuestion formQuestions)
    {
        StackLayout stack = new StackLayout
        {
            Orientation = StackOrientation.Horizontal,
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        Label label = new Label
        {
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.CenterAndExpand
        };
        label.SetBinding(Label.TextProperty, "Text");
        DatePicker datePicker = new DatePicker();
        datePicker.HorizontalOptions = LayoutOptions.EndAndExpand;
        datePicker.SetBinding(DatePicker.DateProperty, "Value");
        stack.Children.Add(label);
        stack.Children.Add(datePicker);
        return stack;
    }
    /// <summary>
    /// Little hack, that unfortunately I had to implement. Because DropDown contains list of DataSource Properties that is essentially array of objects, I had to work around with pulling data of selected item and only
    /// get Values property value. This couldnt be bound correctly, because Values are expecting to be string, but SelectedItem property of picker is an actuall full Object of DataSource. 
    /// I Could probably work around it by Implementing Converter, but Converter will have more code, and I couldnt be bothered. 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Picker_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Cast sender object to BindablePicker (since sender is BindablePicker)
        var picker = (BindablePicker)sender;
        ///Get SelectedItem Value and cast it to DataSource (since SelectedItem is DataSource)
        var value = (string)picker.SelectedItem;
        // Assign Values value back to My Collection
        ((FormQuestion)BindingContext).Value = value;
    }
    /// <summary>
    /// Determine which stack should be created, depending on QuestionType
    /// </summary>
    /// <param name="formModel"></param>
    private void CreateViews(FormQuestion formModel)
    {
        StackLayout newStack = new StackLayout();
                switch (formModel.QuestionType.Name)
                {
                    case "text":
                    newStack.Children.Add(CreateQuestionStackText(formModel));
                        break;
                    case "number":
                    newStack.Children.Add(CreateQuestionStackNumber(formModel));
                        break;
                    case "dropDown":
                    newStack.Children.Add(CreateQuestionStackDropDown(formModel));
                        break;
                    case "date":
                    newStack.Children.Add(CreateQuestionStackDate(formModel));
                        break;
                    default:
                    newStack.Children.Add(CreateQuestionStackText(formModel));
                    break;
                }
        this.mainStack.Children.Add(newStack);
    }
    #endregion

    public class FormQuestion
{
    public int FormQuestionId { get; set; }
    public string Text { get; set; }
    public string Value { get; set; }
    public int QuestionTypeId { get; set; }
    public virtual QuestionType QuestionType { get; set; }
    public int FormId { get; set; }
    public Form Form { get; set; }
    public DataSource DataSource { get; set; }
}
   public class QuestionType
{
    public int QuestionTypeId { get; set; }

    public string Name { get; set; }
}
private FormQuestion formModel{get;set;}
公共表格()
{
初始化组件();
//连接事件,设置主堆栈的布局
创建视图(formModel);
主堆栈方向=堆栈方向垂直;
mainStack.VerticalOptions=LayoutOptions.FillAndExpand;
mainStack.horizontalpoptions=LayoutOptions.FillAndExpand;
}
#端区
/// 
///方法,该方法返回堆栈布局,如果QuestionType为Text,则该堆栈布局包含填充表单所需的控件。
///此方法创建StackLayout、标签和条目,然后返回它。
/// 
/// 
/// 
私有堆栈布局CreateQuestionStackText(FormQuestion formQuestions)
{
StackLayout stack=新的StackLayout
{
方向=堆叠方向。水平,
垂直选项=布局选项。中心,
水平选项=LayoutOptions.FillAndExpand
};
标签=新标签
{
水平选项=布局选项。开始,
垂直选项=LayoutOptions.CenterAndExpand
};
label.SetBinding(label.TextProperty,“Text”);
条目=新条目
{
HorizontalOptions=LayoutOptions.EndAndExpand,
垂直选项=LayoutOptions.CenterAndExpand,
宽度请求=200
};
entry.SetBinding(entry.TextProperty,“Value”);
stack.Children.Add(标签);
stack.Children.Add(条目);
返回栈;
}
/// 
///方法,该方法返回堆栈布局,如果QuestionType为Number,则该堆栈布局包含填充表单所需的控件。
///此方法创建StackLayout、标签和条目,强制使用数字键盘,然后返回数字键盘。
/// 
/// 
/// 
私有堆栈布局CreateQuestionStackNumber(FormQuestion formQuestions)
{
StackLayout stack=新的StackLayout
{
方向=堆叠方向。水平,
垂直选项=布局选项。中心,
水平选项=LayoutOptions.FillAndExpand
};
标签=新标签
{
水平选项=布局选项。开始,
垂直选项=LayoutOptions.CenterAndExpand
};
label.SetBinding(label.TextProperty,“Text”);
条目=新条目
{
HorizontalOptions=LayoutOptions.EndAndExpand,
垂直选项=LayoutOptions.CenterAndExpand,
键盘=键盘。数字,
宽度请求=200
};
entry.SetBinding(entry.TextProperty,“Value”);
stack.Children.Add(标签);
stack.Children.Add(条目);
返回栈;
}
/// 
///方法,该方法返回堆栈布局,如果QuestionType为DropDown,则该堆栈布局包含填充表单所需的控件
///此方法创建StackLayout、Label和BindablePicker,这是我创建的自定义选择器,用于处理MVVM,然后返回它。
/// 
/// 
/// 
私有堆栈布局CreateQuestionStackDropDown(FormQuestion formQuestions)
{
StackLayout stack=新的StackLayout
{
方向=堆叠方向。水平,
垂直选项=布局选项。中心,
水平选项=LayoutOptions.FillAndExpand
};
标签=新标签
{
水平选项=布局选项。开始,
垂直选项=LayoutOptions.CenterAndExpand
};
label.SetBinding(label.TextProperty,“Text”);
BindablePicker-picker=新的BindablePicker();
picker.horizontalpoptions=LayoutOptions.EndAndExpand;
picker.BindingContext=this;
var bind=this.BindingContext;
var strings=formQuestions.DataSource.Values;
var listOfStrings=strings.Split(“;”);
列表结果=新列表();
foreach(listOfStrings中的var项)
{
结果.添加(项目);
}
picker.ItemsSource=结果;
picker.SelectedIndexChanged+=选择器\u SelectedIndexChanged;
//SetBinding(BindablePicker.DisplayMemberPathProperty,新绑定(“值”,BindingMode.TwoWay,新值StoListOfStringsConverter(),null));
picker.SetBinding(BindablePicker.SelectedItemProperty,“值”);
picker.WidthRequest=200;
stack.Children.Add(标签);
stack.Children.Add(选择器);
返回栈;
}
/// 
///方法,该方法返回堆栈布局,如果QuestionType是Data,则该堆栈布局包含填充表单所需的控件
///此方法创建StackLayout、Label和DataPicker并返回它。
/// 
/// 
/// 
私有堆栈布局CreateQuestionStackDate(FormQuestion formQuestions)
{
StackLayout stack=新的StackLayout
{
方向=堆叠方向。水平,
垂直选项=布局选项。中心,
水平选项=LayoutOptions.FillAndExpand
};
标签=新标签
{
水平选项=布局选项。开始,
垂直选项=LayoutOptions.CenterAndExpand
};
label.SetBinding(label.TextProperty,“Text”);
DatePicker DatePicker=新的日期选择器();
datePicker.HorizontalOptions=LayoutOptions.EndAndExpand;
datePicker.SetBinding(datePicker.DateProperty,“值”);
stack.Children.Add(标签);
stack.Children.Add(日期选择器);
返回栈;
}
/// 
///不幸的是,我不得不实施这个小技巧。因为下拉列表包含了D的列表
            <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
    <Frame BorderColor="Gray" CornerRadius="2" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >
    <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" x:Name="mainStack">
    </StackLayout>
    </Frame>
    </StackLayout>