【问题标题】:How to write an Int value from struct to Xaml Page Text Box?如何将结构中的 Int 值写入 Xaml 页面文本框?
【发布时间】:2017-12-03 09:47:28
【问题描述】:

我有这样的结构

struct struValues
{
public int a;
}

我想将它写入 Xaml 页面中的文本单元格。我该怎么做? 我试过了

{Bind struValues.a} 

但它没有用。

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

【问题讨论】:

  • 怎么没用?尝试包含重现问题所需的最短代码。见:minimal reproducible example
  • 我不知道,文本将为空。我应该做类似 xlmns: 的事情吗?
  • 将您的 XAML 的简化版本添加到问题中,足以重现问题。
  • 我已添加到主题中,等待您的消息
  • 尝试将值放入控件的属性中。像这样:&lt;TextBlock Text="{Binding SomeValue}" /&gt;。 XAML 页面需要绑定到视图模型 - 在您的情况下,Durumpg。因此,在该类的构造函数中,绑定到视图模型。视图模型需要通过属性公开结构及其值(在我的示例中,这将是SomeValue)。更重要的是 - 这是支持任何 XAML/WPF 开发的基本 MVVM 概念,因此我强烈建议您获取一本书或教程,因为 SO 不是一个教程站点,它更像是一个问题解决网站。

标签: c# forms xaml xamarin structure


【解决方案1】:

这是一个 MVVM 示例,说明了我如何将 TextCell“Text”属性绑定到结构类型实例的 int 属性。 我为重要的线路放了 cmets。

视觉结果应该是一个表格视图,其中一个部分标题为“Cool Struct Section”,一个文本单元格作为子项,显示文本“123”,这是结构的当前值。

XAML 页面内容:

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

后面的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(也是页面的 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”类,继承自 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));
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-04
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 2014-02-21
    • 1970-01-01
    • 2013-11-21
    相关资源
    最近更新 更多