Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/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.forms_Xamarin.ios - Fatal编程技术网

C# Xamarin:选择器标题不会以编程方式更改

C# Xamarin:选择器标题不会以编程方式更改,c#,xamarin.forms,xamarin.ios,C#,Xamarin.forms,Xamarin.ios,我正在开发一款iOS应用程序,是用C#Xamarin编写的。我在MVVM体系结构中使用了一个选择器,并希望更改选择器标题。 但是当我用OnAddMaterial更改Pickers标题时,标题不会更改 ViewModel: private void OnAddNewMaterial() { SelectedMaterialIndex = -1; MaterialPickerTitle = "New Material";

我正在开发一款iOS应用程序,是用C#Xamarin编写的。我在MVVM体系结构中使用了一个选择器,并希望更改选择器标题。 但是当我用OnAddMaterial更改Pickers标题时,标题不会更改

ViewModel:

private void OnAddNewMaterial()
        {
            SelectedMaterialIndex = -1;
            MaterialPickerTitle = "New Material";
        }

    private string _materialPickerTitle { get; set; }
            public string MaterialPickerTitle
            {
                get { return _materialPickerTitle; }
                set
                {
                    _materialPickerTitle = value;
                    OnPropertyChanged();

                }
            }
观点:

<Picker Title="{Binding MaterialPickerTitle}" Margin="12,4,4,4" Grid.Row="0" Grid.Column="0" HorizontalOptions="FillAndExpand" ItemsSource="{Binding Materials}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedMaterial}" SelectedIndex="{Binding SelectedMaterialIndex}" />

我使用VisualStudio2019

编辑:
初始化视图时,我从选择器设置标题。这很有效。之后,我将从Picker向ItemSource分配对象。当我尝试设置选择器标题时,它不起作用。

我编写了一个演示,更改selectedItem后,可以更改选择器标题。以下是您可以参考的代码:

public partial class MainPage : ContentPage
{
    List<string> monkeyList = new List<string>();

    TestModel model = new TestModel();
    public MainPage()
    {
        InitializeComponent();

        monkeyList.Add("Baboon");
        monkeyList.Add("Capuchin Monkey");
        monkeyList.Add("Blue Monkey");
        monkeyList.Add("Squirrel Monkey");
        monkeyList.Add("Golden Lion Tamarin");
        monkeyList.Add("Howler Monkey");
        monkeyList.Add("Japanese Macaque");

        picker.ItemsSource = monkeyList;

        model.MaterialPickerTitle = "123";
        model.SelectedMaterialIndex = 2;
        BindingContext = model;
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        monkeyList.Add("Baboonww");

        model.SelectedMaterialIndex = -1;
        model.MaterialPickerTitle = "456";

    }
}

class TestModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public TestModel()
    {

    }

    string materialPickerTitle;
    public string MaterialPickerTitle
    {
        set
        {
            if (materialPickerTitle != value)
            {
                materialPickerTitle = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("MaterialPickerTitle"));
                }
            }
        }
        get
        {
            return materialPickerTitle;
        }
    }

    int selectedMaterialIndex;
    public int SelectedMaterialIndex
    {
        set
        {
            if (selectedMaterialIndex != value)
            {
                selectedMaterialIndex = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectedMaterialIndex"));
                }
            }
        }
        get
        {
            return selectedMaterialIndex;
        }
    }
}
public分部类主页面:ContentPage
{
List monkeyList=新列表();
TestModel model=新的TestModel();
公共主页()
{
初始化组件();
monkeyList.添加(“狒狒”);
monkeyList.Add(“卷尾猴”);
monkeyList.Add(“蓝猴”);
添加(“松鼠猴”);
monkeyList.Add(“金狮罗望子”);
添加(“吼猴”);
添加(“日本猕猴”);
picker.ItemsSource=monkeyList;
model.MaterialPickerTitle=“123”;
model.SelectedMaterialIndex=2;
BindingContext=模型;
}
已单击私有无效按钮(对象发送者,事件参数e)
{
monkeyList.Add(“Baboonww”);
model.SelectedMaterialIndex=-1;
model.MaterialPickerTitle=“456”;
}
}
类TestModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共测试模型()
{
}
字符串材料标题;
公共字符串材质标题
{
设置
{
if(materialPickerTitle!=值)
{
materialPickerTitle=值;
if(PropertyChanged!=null)
{
已变更的财产(即新的财产变更所有权(“材料所有权”);
}
}
}
得到
{
返回物料标题;
}
}
int-selectedMaterialIndex;
公共int-SelectedMaterialIndex
{
设置
{
如果(selectedMaterialIndex!=值)
{
selectedMaterialIndex=值;
if(PropertyChanged!=null)
{
PropertyChanged(这是新PropertyChangedEventArgs(“SelectedMaterialIndex”);
}
}
}
得到
{
返回selectedMaterialIndex;
}
}
}
在xaml中:

<StackLayout>
    <!-- Place new controls here -->
    <Button Clicked="Button_Clicked" Text="click to change title" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />

    <Picker x:Name="picker"
    Title="{Binding MaterialPickerTitle}"
    SelectedIndex="{Binding SelectedMaterialIndex}"
    TitleColor="Red">
    </Picker>
</StackLayout>


请检查项目中的绑定。如果标题更改,请添加一些断点以进行调试。我还上传了我的示例项目。

ViewModel是否实现了INotifyPropertyChanged?是的,在我在ViewModel中更改了其他所有值后,视图中的所有值都在更新。我正在使用MVVMHelper您是否尝试过以两种方式设置标题的绑定模式?我尝试过,没有更改。我解决了它。显然,在设置选择器的SelectedItem后,您无法更改标题。现在我不再向选择器添加SelectedItem,而是直接设置标题。感谢您的详细回答!现在我有了