使用消息中心 xamarin 表单 PCL 设置当前对象值
Using Messaging center xamarin forms PCL to set current objects value
我有一个以编程方式创建入口控件的场景。
foreach (var control in FormInfo.FormElementsInfo)
{
case "textbox":
//Some code
break;
case "dropdown":
Entry objDropdown = new Entry();
objDropdown.HeightRequest = 40;
objDropdown.StyleId = Convert.ToString(control.ElementId);
objDropdown.SetBinding(Entry.TextProperty, "ElementValue",BindingMode.TwoWay);
objDropdown.BindingContext = control;
layout.Children.Add(objDropdown);
MessagingCenter.Subscribe<Picklists, string>(objDropdown, "PicklistSelected", (sender, arg) =>
{
objDropdown.Text = arg;
// I tried this too as this is two way binding. It didn't show the value.
//control.ElementValue = arg;
} );
break;
}
如果我单击任何条目,它将打开一个列表视图。一旦我 select 列表视图中的选项,它将在条目中填充该数据。
但这应该仅在当前条目中显示 selected 值,但它正在更改所有条目中的值。
如何避免这种情况。我希望只在当前条目中填充 selected 值。
如有任何建议,我们将不胜感激。谢谢。
===更清晰的问题=====
如果我们使用 2 种方式绑定以编程方式创建 n 个 Entry 控件。是否可以更改 selecting 其他页面上的单个条目值?如果是,如何实现?
FormInfo
public class FormInfo
{
public List<FormsElementInfo> FormElementsInfo { get; set; }
}
FormsElementInfo
public class FormsElementInfo : INotifyPropertyChanged
{
private string _elementValue;
public string ElementValue {
get => _elementValue;
set {
if(_elementValue != value)
{
_elementValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ElementValue"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
内容页
public class ListWhosebug : ContentPage
{
private FormInfo _info = new FormInfo
{
FormElementsInfo = new List<FormsElementInfo>()
{
new FormsElementInfo { ElementValue = "test 1"},
new FormsElementInfo { ElementValue = "test 2"},
new FormsElementInfo { ElementValue = "test 3"},
new FormsElementInfo { ElementValue = "test 4"},
}
};
private StackLayout _stack = new StackLayout();
private List<string> _source = new List<string>
{
"output 1","output 2","output 3","output 4",
};
public ListWhosebug()
{
//BindingContext = _info;
foreach(var c in _info.FormElementsInfo)
{
Entry tempEntry = new Entry
{
HeightRequest = 40,
Placeholder = c.ElementValue,
BindingContext = c
};
tempEntry.SetBinding(Entry.TextProperty, "ElementValue");
_stack.Children.Add(tempEntry);
}
ListView _lv = new ListView { ItemsSource = _source };
_lv.ItemSelected += Lv_ItemSelected;
_stack.Children.Add(_lv);
Content = _stack;
}
private void Lv_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var selectedElement = e.SelectedItem.ToString();
var index = _source.IndexOf(selectedElement);
var entry = _info.FormElementsInfo[index];
entry.ElementValue = selectedElement;
}
}
输出
在列表视图中选择相应的索引将为同一索引更新"ElementValue"。
首先感谢@Joshua Poling 抽出时间来帮助我。
我认为 MessagingCenter 不适合这种方式。
我正在为每个元素分配一个唯一的 styleId,我 create.That 基本上存储堆栈布局中的位置。
我写了一个委托,其中 returns 选定的值以及元素的位置。因为该元素始终是触发此事件的条目。我使用下面的代码来实现这一点。
条目 myentry = (Xamarin.Forms.Entry)layout.Children[src.ElementId];
我有一个以编程方式创建入口控件的场景。
foreach (var control in FormInfo.FormElementsInfo)
{
case "textbox":
//Some code
break;
case "dropdown":
Entry objDropdown = new Entry();
objDropdown.HeightRequest = 40;
objDropdown.StyleId = Convert.ToString(control.ElementId);
objDropdown.SetBinding(Entry.TextProperty, "ElementValue",BindingMode.TwoWay);
objDropdown.BindingContext = control;
layout.Children.Add(objDropdown);
MessagingCenter.Subscribe<Picklists, string>(objDropdown, "PicklistSelected", (sender, arg) =>
{
objDropdown.Text = arg;
// I tried this too as this is two way binding. It didn't show the value.
//control.ElementValue = arg;
} );
break;
}
如果我单击任何条目,它将打开一个列表视图。一旦我 select 列表视图中的选项,它将在条目中填充该数据。 但这应该仅在当前条目中显示 selected 值,但它正在更改所有条目中的值。
如何避免这种情况。我希望只在当前条目中填充 selected 值。
如有任何建议,我们将不胜感激。谢谢。
===更清晰的问题=====
如果我们使用 2 种方式绑定以编程方式创建 n 个 Entry 控件。是否可以更改 selecting 其他页面上的单个条目值?如果是,如何实现?
FormInfo
public class FormInfo
{
public List<FormsElementInfo> FormElementsInfo { get; set; }
}
FormsElementInfo
public class FormsElementInfo : INotifyPropertyChanged
{
private string _elementValue;
public string ElementValue {
get => _elementValue;
set {
if(_elementValue != value)
{
_elementValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ElementValue"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
内容页
public class ListWhosebug : ContentPage
{
private FormInfo _info = new FormInfo
{
FormElementsInfo = new List<FormsElementInfo>()
{
new FormsElementInfo { ElementValue = "test 1"},
new FormsElementInfo { ElementValue = "test 2"},
new FormsElementInfo { ElementValue = "test 3"},
new FormsElementInfo { ElementValue = "test 4"},
}
};
private StackLayout _stack = new StackLayout();
private List<string> _source = new List<string>
{
"output 1","output 2","output 3","output 4",
};
public ListWhosebug()
{
//BindingContext = _info;
foreach(var c in _info.FormElementsInfo)
{
Entry tempEntry = new Entry
{
HeightRequest = 40,
Placeholder = c.ElementValue,
BindingContext = c
};
tempEntry.SetBinding(Entry.TextProperty, "ElementValue");
_stack.Children.Add(tempEntry);
}
ListView _lv = new ListView { ItemsSource = _source };
_lv.ItemSelected += Lv_ItemSelected;
_stack.Children.Add(_lv);
Content = _stack;
}
private void Lv_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var selectedElement = e.SelectedItem.ToString();
var index = _source.IndexOf(selectedElement);
var entry = _info.FormElementsInfo[index];
entry.ElementValue = selectedElement;
}
}
输出
在列表视图中选择相应的索引将为同一索引更新"ElementValue"。
首先感谢@Joshua Poling 抽出时间来帮助我。
我认为 MessagingCenter 不适合这种方式。
我正在为每个元素分配一个唯一的 styleId,我 create.That 基本上存储堆栈布局中的位置。
我写了一个委托,其中 returns 选定的值以及元素的位置。因为该元素始终是触发此事件的条目。我使用下面的代码来实现这一点。
条目 myentry = (Xamarin.Forms.Entry)layout.Children[src.ElementId];