Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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# 沙马林形成了一场恶梦_C#_Xamarin.forms - Fatal编程技术网

C# 沙马林形成了一场恶梦

C# 沙马林形成了一场恶梦,c#,xamarin.forms,C#,Xamarin.forms,我真不敢相信我花了一天的时间来让它工作!我从上个月开始在Xamarin开发表单,大部分情况下都很顺利。我的背景是ASP.NETMVC和Windows窗体,所以MVVM概念对我来说是新概念 我遇到的问题非常令人沮丧,而且看起来很愚蠢。我想这可能是我的实现,因为我没有使用MVVM,而是在代码背后执行所有绑定?我有一个页面,它有一个搜索框和一个ListView控件。ListView中的每个项都相当简单。。。它只包含几个标签和一个开关。每次有人执行搜索时,都会绑定ListView。我遇到的问题是,每次有

我真不敢相信我花了一天的时间来让它工作!我从上个月开始在Xamarin开发表单,大部分情况下都很顺利。我的背景是ASP.NETMVC和Windows窗体,所以MVVM概念对我来说是新概念

我遇到的问题非常令人沮丧,而且看起来很愚蠢。我想这可能是我的实现,因为我没有使用MVVM,而是在代码背后执行所有绑定?我有一个页面,它有一个搜索框和一个ListView控件。ListView中的每个项都相当简单。。。它只包含几个标签和一个开关。每次有人执行搜索时,都会绑定ListView。我遇到的问题是,每次有人执行搜索时,它都会刷新模型,所有这些都是注定的。。。这进而触发开关切换事件。在Toggle事件中,我有一些代码可以在值更改时更新数据库,所以我不希望每次模型更改时都触发该代码。这是非常令人沮丧的,因为似乎没有点击事件。请参见下面的Xaml和我正在使用的方法:

    public async void SearchItems()
    {
        PageLoaded = false;
        if(string.IsNullOrEmpty(this.txtSearch.Text)) { return; }
        this.LoadSearch.IsRunning = true;
        // Populate Locations For Item
        string ItemType = HttpUtility.UrlEncode(this.SelectItemType.SelectedItem.ToString());
        string Search = HttpUtility.UrlEncode(this.txtSearch.Text.ToLower());
        string url = string.Format("https://test.com/api/LocationsSearch?itemType={0}&search={1}", ItemType, Search);
        LocationsList = new List<Locations>();
        var json = await func.GetIBApi(url);
        LocationsList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Locations>>(json);
        this.ItemsList.ItemsSource = LocationsList;
        this.LoadSearch.IsRunning = false;
        PageLoaded = true;
    }
XAML:


使用另一个锁:

 if (PageLoaded && !_internal)
                        {
                            //entered handler
                            _internal=true;
                            bool isPrimary = e.Value;
                            _internal=false;

缺少一些代码,iPrimary set在哪里?@NickKovalsky请参阅已修改,谢谢。添加PageLoaded后,在搜索操作期间仍会触发切换事件?不应该。你能给我们提供一个能重现问题的样品吗?谢谢。。。我尝试了一些类似的东西,我知道这并不明显,因为我错过了切换事件。在我的代码中,属性标志称为PageLoaded,我在搜索方法的开始和结束处设置它。有什么想法吗?这个想法是当PageLoaded=true时设置IsPrimary时,您的混乱就出现了;你基本上调用了处理程序两次,从内部调用。不幸的是,这给了我同样的结果。事实上,每次我执行搜索时,它都会命中事件。查看您的代码,您的意思是,如果(PageLoaded)当PageLoaded==false时,它会执行其中的代码,对吗?
    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="IBMobile.Views.LocationsPage"
             xmlns:local2="clr-namespace:FontAwesome"
             Visual="Material">
    <ContentPage.IconImageSource>
        <FontImageSource FontFamily="{StaticResource FontAwesomeSolid}" Glyph="{x:Static local2:IconFont.MapMarkerAlt}" />
    </ContentPage.IconImageSource>
    <ContentPage.Content>
        <StackLayout Padding="25">
            <Picker  x:Name="SelectItemType" SelectedIndexChanged="SelectItemType_SelectedIndexChanged" />
            <SearchBar x:Name="txtSearch" SearchButtonPressed="TxtSearch_SearchButtonPressed"  Placeholder="Search..." FontSize="Title"></SearchBar>
            <ActivityIndicator Color="#007D5D" IsRunning="false" x:Name="LoadSearch"  HorizontalOptions="Center" />
            <ListView x:Name="ItemsList" ItemTapped="ItemsList_ItemTapped"  HasUnevenRows="True" BackgroundColor="White">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout VerticalOptions="FillAndExpand">
                                <Grid VerticalOptions="FillAndExpand" Padding="0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="3*"></ColumnDefinition>
                                        <ColumnDefinition Width="3*"></ColumnDefinition>
                                        <ColumnDefinition Width="1*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                    </Grid.RowDefinitions>

                                    <Label TextColor="#007D5D" Text="{Binding ViewID}" FontSize="Title" VerticalTextAlignment="Center" Grid.Column="0" Grid.Row="0"></Label>
                                    <Label TextColor="#007D5D" Text="{Binding Location}" FontSize="Title" VerticalTextAlignment="Center" Grid.Column="1" Grid.Row="0"></Label>
                                    <Label Text="Primary" Grid.Column="2" Grid.Row="0"></Label>
                                    <Switch IsToggled="{Binding IsPrimary}" x:Name="Toggle1" ThumbColor="#007D5D" Grid.Column="2" Grid.Row="1" Toggled="Switch_Toggled"/>
                                    <Label  Text="{Binding Description}"  Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"></Label>
                                    <Label  Text="{Binding Message}"  Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="3" FontSize="Small" TextColor="Red"></Label>
                                </Grid>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
    if (PageLoaded)
                    {
                        //entered handler
                        bool isPrimary = e.Value; //we are invoking the handler again from inside itsself
                                                  // all the mess comes from this
 if (PageLoaded && !_internal)
                        {
                            //entered handler
                            _internal=true;
                            bool isPrimary = e.Value;
                            _internal=false;