Prism Xamarin Forms View在设置值并在viewmodel中调用RaisePropertyChanged后没有更新?

时间:2017-11-30 09:59:22

标签: xamarin.forms

我无法将值绑定到Viewmodel中的View,我可以看到值设置(ProductList)并在调试时调用RaisePropertyChanged;我相信代码是正确的,因为虽然我将它设置为App.cs中的第一页(NavigationService.NavigateAsync(" StudentPage / NavigationPage / StudentRedeemPage");),但是当它被调用/导航时它工作但不起作用StudentRedeemPage的另一页。

的Xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="mySeeds.Views.StudentRedeemPage"
             Title="Redeem">
    <ContentPage.Content>
        <Grid x:Name="MainGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="2*" />
                <RowDefinition Height="3*" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" 
                  Grid.RowSpan="1" 
                  HorizontalOptions="FillAndExpand" 
                  VerticalOptions="FillAndExpand" 
                  BackgroundColor="{StaticResource RedeemBanner}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="3*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="6*" />
                    <ColumnDefinition Width="4*" />
                </Grid.ColumnDefinitions>

                <Image Source="Icon" 
                       VerticalOptions="Center" 
                       HorizontalOptions="Center" 
                       Grid.Column="1"
                       Grid.Row="0"/>
                <StackLayout Grid.Column="0"
                             Grid.Row="0"
                             HorizontalOptions="CenterAndExpand" 
                             VerticalOptions="Center">
                    <Label Text="REWARD SHOP"
                           TextColor="White"
                           FontSize="Medium"
                           FontAttributes="Bold"
                           VerticalTextAlignment="Center"
                           LineBreakMode="TailTruncation"/>
                    <Label Text="{Binding User_Profile_Table.Points_Earned, StringFormat='{0} Seeds'}"
                           TextColor="White"
                           FontSize="Large"
                           FontAttributes="Bold"
                           VerticalTextAlignment="Center"
                           LineBreakMode="WordWrap"/>
                </StackLayout>
                <StackLayout Grid.Row="1" Grid.ColumnSpan="2" >
                    <Label Text="Partners" BackgroundColor="{StaticResource RedeemTab}" HorizontalOptions="FillAndExpand" VerticalOptions="Center" />
                    <ListView 
                            Margin="0,0,0,0" 
                            HorizontalOptions="FillAndExpand" 
                            VerticalOptions="FillAndExpand" 
                            ItemsSource="{Binding ProductList}"
                            HasUnevenRows="true"
                            CachingStrategy="RecycleElement">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Grid Padding="0, 0, 10, 0">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="30"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Image x:Name="Img"
                                       Grid.Row="0" Grid.Column="0"
                                       HorizontalOptions="Center"
                                       VerticalOptions="Center"
                                       HeightRequest="30"
                                       WidthRequest="30"
                                       Source="{Binding ImageUrl}"/>
                                        <StackLayout Grid.Row="0" Grid.Column="1" VerticalOptions="Center">
                                            <Label Text="{Binding SalesPriceIncVat,StringFormat='{0} Seeds'}"
                                       TextColor="{StaticResource RedeemStatusBar}"
                                       FontAttributes="Bold"
                                       VerticalOptions="Center"/>
                                            <Label Text="{Binding Name}"
                                       VerticalOptions="Center"/>
                                        </StackLayout>
                                        <Button x:Name="btnRedeem"
                                        Text="GET IT!"
                                        TextColor="White"
                                        BackgroundColor="{StaticResource RedeemLightGreen}"
                                        Grid.Row="0" Grid.Column="2">

                                        </Button>
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

                </StackLayout>
            </Grid>
        </Grid>
    </ContentPage.Content>
</ContentPage>

视图模型

public class StudentRedeemPageViewModel : NavigationViewModelBase
{
    private List<Data> _productList;
    public List<Data> ProductList
    {
        get { return _productList; }
        set
        {
            SetProperty(ref _productList, value);
            RaisePropertyChanged(nameof(ProductList));
        }
    }

    private User_Profile_Table user_Profile_Table;
    public User_Profile_Table User_Profile_Table
    {
        get { return user_Profile_Table; }
        set
        {
            SetProperty(ref user_Profile_Table, value);
            RaisePropertyChanged(nameof(User_Profile_Table));
        }
    }

    private ProductModel _product_Table;
    public ProductModel Product_Table
    {
        get { return _product_Table; }
        set
        {
            SetProperty(ref _product_Table, value);
            RaisePropertyChanged(nameof(Product_Table));
        }
    }

    public DelegateCommand GetProductListCommand { get; private set; }
    public DelegateCommand UserProfileCommand { get; private set; }

    public DelegateCommand NavigateToRedeemPage { get; set; }

    private IRequestProvider _requestProvider;

    public StudentRedeemPageViewModel(IDeviceService deviceService, INavigationService navigationService,
        IPageDialogService pageDialogService, IRequestProvider requestProvider)
        : base(deviceService, navigationService, pageDialogService)
    {

        _requestProvider = requestProvider;

        NavigateToRedeemPage = new DelegateCommand(() => NavigateCommand.Execute("VocherPage"));

        GetProductListCommand = new DelegateCommand(async () => await GetProductList());
        UserProfileCommand = new DelegateCommand(async () => await GetUserProfile());


        UserProfileCommand.Execute();
        GetProductListCommand.Execute();
    }
    async Task GetProductList()
    {
        List<Data> Test = new List<Data>();
        string username = "admin@xxxx.com";
        string password = "xxxxxxxxx";
        string svcCredentials = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));

        if (svcCredentials != null)
        {
            var client = new HttpClient();
            client.BaseAddress = new Uri("https://xxxxxx.com/");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", svcCredentials);
            var productsResponse = await client.GetAsync("xxxxxx/api/xxxx/");
            var productsJson = productsResponse.Content.ReadAsStringAsync().Result;
            ProductModel members = JsonConvert.DeserializeObject<ProductModel>(productsJson);
            ProductList = members.Data;
        }
    }

    async Task GetUserProfile()
    {
        User_Profile_Table = await _requestProvider.GetAsync<User_Profile_Table>(
            GlobalSetting.Instance.UserProfileTableEndpoint, Settings.AuthAccessToken);
    }
}

模型

public class ProductModel
{
    public List<Data> Data { get; set; }
    public string FirstItem { get; set; }
    public string HasNextPage { get; set; }
    public string HasPreviousPage { get; set; }
    public string LastItem { get; set; }
    public string PageNumber { get; set; }
    public string PageSize { get; set; }
    public string TotalItems { get; set; }
    public string TotalPages { get; set; }
}

public class Data
{
    [JsonProperty("Data")]
    public string ProductId { get; set; }
    public string PartnerId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string SalesPriceIncVat { get; set; }
    public string SalesPriceExcVat { get; set; }
    public string SalesVat { get; set; }
    public string ValidFromDateUtc { get; set; }
    public string ValidToDateUtc { get; set; }
    public string TermsAndConditions { get; set; }
    public string UsageInstructions { get; set; }
    public string SlaTime { get; set; }
    public string SlaText { get; set; }
    public string Units { get; set; }
    public string StockRequired { get; set; }
    public string UseVariableValues { get; set; }
    public string MinVariableValue { get; set; }
    public string MaxVariableValue { get; set; }
    public string VariableValueAllowedIncrements { get; set; }
    public string ImageUrl { get; set; }
    public string ImageCaption { get; set; }
    public string CurrentStockBalance { get; set; }
}

1 个答案:

答案 0 :(得分:0)

如何将viewmodel绑定到您的视图,上面没有bindingcontext。