简体   繁体   English

如何将Int值从struct写入Xaml页面文本框?

[英]How to write an Int value from struct to Xaml Page Text Box?

I have a structure like this 我有这样的结构

struct struValues
{
public int a;
}

I want to write it to a textcell in Xaml Page. 我想将其写入Xaml页面中的文本单元。 How can I do it? 我该怎么做? I tried 我试过了

{Bind struValues.a} 

but it did not worked. 但是没有用。

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:i18n="clr-namespace:test_Trade.Localization;assembly=test_Trade"
             xmlns:puser="clr-namespace:test_Trade.Classes"
             x:Class="test_Trade.Views.Durumpg">
    <ContentPage.Content>
        <TableView Intent="Form">
            <TableRoot>
                <TableSection Title="{i18n:TranslateExtension Text=Stats}">
                    <ImageCell ImageSource="Euro.png" Detail="{Here Should be money}" x:Name="imgCelleuro"/>

                </TableSection>
            </TableRoot>
        </TableView>
    </ContentPage.Content>
</ContentPage>

Here is an MVVM example of how I would bind a TextCell "Text" Property to an int property of a struct-typed instance. 这是一个MVVM示例,说明如何将TextCell“ Text”属性绑定到结构类型实例的int属性。 I put out comments for important lines. 我对重要内容发表了评论。

The visual outcome should be a table view with one section titled as "Cool Struct Section" and one Text Cell as a child, showing the text "123" which is the structs current value. 视觉结果应该是一个表格视图,其中一个标题为“ Cool Struct Section”的区域,一个文本单元格作为子级,显示文本“ 123”,它是结构的当前值。

XAML Page Content: XAML页面内容:

<ContentPage.Content>
        <TableView Intent="Settings">
            <TableRoot>
                <TableSection Title="{Binding TableSectionTitle}">
                    <TextCell Text="{Binding StruValues.A}" />
                </TableSection>
            </TableRoot>
        </TableView>
</ContentPage.Content>

C# Page code behind: C#页面代码背后:

using MVVMExample.ViewModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MVVMExample
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TableViewPage : ContentPage
    {
        public TableViewPage()
        {
            InitializeComponent();
            BindingContext = new TableViewPageVM(); //Assing the ViewModel to the binding context!
        }
    }
}

C# ViewModel (also BindingContext of the Page) : C#ViewModel (也是页面的BindingContext)

using MVVMExample.Utils;

namespace MVVMExample.ViewModels
{
    public class TableViewPageVM : BindableBase
    {
        //Simple text to bind to the TableSection Title property
        private string tableSectionTitle;
        public string TableSectionTitle { get { return tableSectionTitle; } set { SetProperty(ref tableSectionTitle, value); } }

        //Property that will hold our struValues instance. The TextCell "Text" Property will be bound to the A property of this instance.
        //The A property exposes the value of the actual "a" property of the facades struct instance
        private struValuesFacade _struValues;
        public struValuesFacade StruValues { get { return _struValues; } set { SetProperty(ref _struValues, value); } }

        public TableViewPageVM()
        {
            TableSectionTitle = "Cool Struct Section"; //Set the title text
            StruValues = new struValuesFacade(123);     //Create an instance of our facade
        }

        /// <summary>
        /// A "facade" of the actual struct, that exposes the "a" property of the struct instance
        /// Also holds the instances of the struct
        /// </summary>
        public class struValuesFacade : BindableBase
        {
            struValues origin;
            public int A
            {
                get { return origin.a; }
                set
                {
                    SetProperty(ref origin.a, value);
                }
            }

            public struValuesFacade(int value)
            {
                origin = new struValues() { a = value };
            }
        }

        /// <summary>
        /// Your beloved struct
        /// </summary>
        struct struValues
        {
            public int a;
        }
    }
}

C# "BindableBase" class, inherits from INotifyPropertyChanged (credits to msdn.microsoft.com) (mandatory to update the view when properties change in an MVVM environment) C#“ BindableBase”类继承自INotifyPropertyChanged(提供给msdn.microsoft.com) (强制在MVVM环境中更改属性时更新视图)

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace MVVMTest.Utils
{
    public class BindableBase : INotifyPropertyChanged
    {
        ///
        /// Multicast event for property change notifications.
        ///
        public event PropertyChangedEventHandler PropertyChanged;

        ///
        /// Checks if a property already matches a desired value.  Sets the property and
        /// notifies listeners only when necessary.
        ///
        ///Type of the property.
        ///Reference to a property with both getter and setter.
        ///Desired value for the property.
        ///Name of the property used to notify listeners.  This
        /// value is optional and can be provided automatically when invoked from compilers that
        /// support CallerMemberName.
        ///True if the value was changed, false if the existing value matched the
        /// desired value.
        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }

        ///
        /// Notifies listeners that a property value has changed.
        ///
        ///Name of the property used to notify listeners.  This
        /// value is optional and can be provided automatically when invoked from compilers
        /// that support .
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM