Xamarin AutoSuggestBox文本未从代码隐藏中设置

时间:2019-02-08 03:09:32

标签: c# xaml xamarin

我尝试直接在代码中设置代码,并将其绑定到属性(并触发PropertyChangedEventHandler)。但是前端没有更新。

我采用的方法似乎适用于某些本机控件(我将代码修改为表单上的下一个控件(选择器,将其ItemsSource绑定到根据我尝试的相同值计算出的属性将AutoSuggestBox文本绑定到。

我的xaml看起来像这样。

 <forms:AutoSuggestBox x:Name="locationAutoSuggestBox" 
 PlaceholderText="Enter site"
 TextChanged="LocationAutoSuggestBox_TextChanged"
 QuerySubmitted="LocationAutoSuggestBox_QuerySubmitted"
 SuggestionChosen="LocationAutoSuggestBox_SuggestionChosen"
 Text="{x:Binding SelectedSiteStr, Mode=TwoWay}" />

<Picker x:Name="pickerShift" HorizontalOptions="FillAndExpand" 
Title="Select shift" ItemsSource="{x:Binding ShiftsForLocation}">
</Picker>

我的视图模型代码的相关部分如下所示。

    private Sites selectedSite;
    public Sites SelectedSite
    {
        get
        {
            return selectedSite;
        }
        set
        {
            selectedSite = value;
            if(selectedSite != null)
            {
                selectedSiteStr = selectedSite.SiteName;
                OnPropertyChanged("SelectedSiteStr"); 
                OnPropertyChanged("ShiftsForLocation");
            }
        }
    }

    private string selectedSiteStr = string.Empty;
    public string SelectedSiteStr
    {
        get
        {
            return selectedSiteStr;
        }
        set
        {
            if (value != null)
            {
                selectedSiteStr = value;
                if (AllSites != null)
                {
                    var site = AllSites.FirstOrDefault(x => x.SiteName.ToLower() == value.ToLower());
                    if (site != null)
                    {
                        selectedSite = site;
                    }
                }
            }
        }
    }        

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var changed = PropertyChanged;
        if (changed != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

与ShiftsForLocation的绑定正在工作(它是使用selectedSite计算的,但是对SelectedSiteStr的绑定却无效,我可以跟踪从属性中提取正确值的代码。

我很困惑。任何帮助表示赞赏。

乔什

1 个答案:

答案 0 :(得分:0)

如果有人遇到它,我通过将其从0.1.0升级到0.6.0进行了修复。我必须包括该库的预发行版本,我并不热衷于嵌入式设备,大多数情况下,嵌入式设备将不在移动/ WiFi覆盖范围内,但是它似乎可以正常工作。

乔什