我正面临一个很快就会让我发疯的问题。我有pickermodel
, 有数字。我从选择器中选择一个数字,然后拍照。当我 时dismissmodalviewcontroller
,我可以viewcontroller
在pickermodel
节目中看到我选择的相同号码,但是当我尝试通过代码访问时,它是空的。我正在处理,Xamarin.iOs
但我认为这个问题与 iOS 有关。
//ViewWillAppear 从视图控制器
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
sitepickerModel = new SitePicker();
sitepickerModel.Sites.Add(" ");
Console.WriteLine(this.Title);
//gets sitesID with the name of Title.
sitesID = localDb.GetSitesID(this.Title);
//Adds sites ID(number) to pickermodel
if (sitesID != null)
{
foreach (var s in sitesID)
{
sitepickerModel.Sites.Add(s);
}
}
//assigns pickerview to the class pickerModel
pickerSitesID.Model = sitepickerModel;
sitepickerModel.ValueChanged += SitepickerModel_ValueChanged;
siteLabelTxt.Text = sitepickerModel.SelectedSite;
syncronizeSegment.SelectedSegment = 1;
if (sitepickerModel.SelectedSite != null)
Console.WriteLine("View:" + sitepickerModel.SelectedSite);
}
//CLass UIPickerViewModel
public class SitePicker : UIPickerViewModel
{
public event EventHandler<EventArgs> ValueChanged;
//items to show up in the picker
public List<string> Sites { get; private set; }
public string SelectedSite
{
get
{
if (selectedIndex > Sites.Count && selectedIndex > 0)
{
selectedIndex = Sites.Count;
return Sites[selectedIndex];
}
else if (selectedIndex == 0 )
{
return " ";
}
else if (selectedIndex== Sites.Count) {
selectedIndex = Sites.Count - 1;
return Sites[selectedIndex];
}
else {
if (Sites[selectedIndex] == " ")
return " ";
else
return Sites[selectedIndex];
}
}
}
int selectedIndex = 0;
public SitePicker()
{
Sites = new List<string>();
}
public override nint GetRowsInComponent(UIPickerView pickerView, nint component)
{
return Sites.Count;
}
public override string GetTitle(UIPickerView pickerView, nint row, nint component)
{
if (NoItem())
return " ";
var item = Sites[(int)row];
return item;
}
public override nint GetComponentCount(UIPickerView pickerView)
{
return 1;
}
public override void Selected(UIPickerView pickerView, nint row, nint component)
{
selectedIndex = (int)row;
if (ValueChanged != null)
{
ValueChanged(this, new EventArgs());
}
}
bool NoItem(int row = 0)
{
return Sites == null || row >= Sites.Count;
}
}
}