Xamarin Forms with Prism-使用WebService时遇到问题

时间:2018-09-15 18:57:18

标签: forms xamarin prism webservice-client

有人可以帮助我吗?我在Android(屏幕)上使用Prism i VS2017创建了简单的Xamarin Forms项目。我使用了棱镜模板包。我想用WebService连接项目。 here is a link to screen of all project 我有两个项目PrismCoursApp和PrismCoursApp.Droid。第一个项目包含SecondPageViewModel.cs,在这里我尝试使用连接的WebService(wsMES),但无法使用PrismCoursApp.Droid添加名称空间。 项目PrismCourseApp.Android的名称空间是PrismCourseApp.Droid和 PrismCourseApp.Android取决于PrismCourseApp。
我只能在PrismCoursApp.Android项目中添加对Web服务的引用,但我想在PrismCourseApp的SecondPageViewModel.cs中使用它。

有人可以告诉我我在做什么错吗? 谢谢

SecondPageViewModel.cs
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;

using PrismCourseApp.Models;
using System.Collections.ObjectModel;

namespace PrismCourseApp.ViewModels
{
    public class SecondPageViewModel : BindableBase, INavigationAware
    {
        //zmienna do WebService 
        //wsMES.WSwitoMES ws = new wsMES.WSwitoMES();

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _UserCode;
        public string UserCode
        {
            get { return _UserCode; }
            set { SetProperty(ref _UserCode, value); }
        }

        private string _LokalizCode;
        public string LokalizCode
        {
            get { return _LokalizCode; }
            set { SetProperty(ref _LokalizCode, value); }
        }

        public SecondPageViewModel()
        {
            UserCode = AppStateTest.User;
            LokalizCode = AppStateTest.CurrentCode;

            Title = "Użytkownik/Lokalizacja";


        }


        public void OnNavigatedFrom(INavigationParameters parameters)
        {

        }

        public void OnNavigatedTo(INavigationParameters parameters)
        {
            if (parameters.ContainsKey("par1"))
            {
                string par1 = (string)parameters["par1"];
                string par2 = (string)parameters["par2"];
            }

        }

        public void OnNavigatingTo(INavigationParameters parameters)
        {

        }
    }
}

SecondPage.axml
<?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="PrismCourseApp.Views.SecondPage"
             BackgroundColor="White"
             Title="{Binding Title}"
             xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"  
             xmlns:c="clr-namespace:PrismCourseApp.Converters;assembly=PrismCourseApp">
    <ContentPage.Resources>
        <ResourceDictionary>
            <!--<c:ItemTappedEventArgsConverter x:Key="itemTappedEventArgsConverter" />-->
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout
        Spacing="20">

        <Label
            Text="Zalogowany użytkownik:"
            TextColor="Gray"/>
        <Label 
            Text="{Binding UserCode}"
            FontSize="Large" 
            HorizontalOptions="Center" 
            VerticalOptions="CenterAndExpand" />

        <Label
            Text="Lokalizacja:"
            TextColor="Gray"/>
        <Label 
            Text="{Binding LokalizCode}"
            FontSize="Large" 
            HorizontalOptions="Center" 
            VerticalOptions="CenterAndExpand" />

        <ListView 
            x:Name="lstView">
            <!--ItemsSource="{Binding MyDatas}">-->
            <!--<ListView.Behaviors>
                <b:EventToCommandBehavior EventName="ItemTapped" 
                                          Command="{Binding ItemTappedCommand}"
                                          EventArgsConverter="{StaticResource itemTappedEventArgsConverter}" />
            </ListView.Behaviors>-->
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding name}" Detail="{Binding comment}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>


    </StackLayout>
</ContentPage>

SecondPage.axml.cs
using Xamarin.Forms;
using PrismCourseApp.Models;
using System.Collections.ObjectModel;

namespace PrismCourseApp.Views
{
    public partial class SecondPage : ContentPage
    {    

        //Elementy do ListView (klasa MyDate w PrismCourseApp)
        private ObservableCollection<MyDate> MyDatas { get; set; }

        public SecondPage()
        {
            InitializeComponent();
            MyDatas = new ObservableCollection<MyDate>();
            lstView.ItemsSource = MyDatas;

            for (int i = 0; i < 30; i++)
            {
                MyDatas.Add(new MyDate
                {
                    name = "Pozycja " + (i+1).ToString(),
                    comment = "Miejsce na szczegóły " + (i+1).ToString()
                });
            }
        }    
    }
}

MainActivity.cs in Android Project
using Android.App;
using Android.Content.PM;
using Android.OS;
using Prism;
using Prism.Ioc;

namespace PrismCourseApp.Droid
{
    [Activity(Label = "PrismCourseApp", Icon = "@drawable/ic_launcher", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App(new AndroidInitializer()));
        }
    }

    public class AndroidInitializer : IPlatformInitializer
    {
        public void RegisterTypes(IContainerRegistry container)
        {
            // Register any platform specific implementations

        }
    }
}

1 个答案:

答案 0 :(得分:0)

总结以上评论中的讨论:

不应使Web服务依赖于许多客户端平台之一的特定功能。最好将服务的接口放在服务器和不同客户端实现之间共享的客户端部分之间。

假设您有一个适用于Android和IOS的移动应用程序。然后,您将有两个项目MyApp.DroidMyApp.IOS,分别用于特定于客户端的实现。此外,还有一个项目,它们两个都引用,并且(希望)包含您应用程序的大多数客户端逻辑:MyApp.Logic

服务器的当前位置:您拥有实现服务的MyApp.Server项目。如果需要定义在应用程序和服务之间进行通信的接口(想到了WCF),则可以定义由客户端逻辑(MyApp.Logic)和服务器实现(MyApp.Server引用的项目。 ):MyApp.Interface

MyApp.DroidMyApp.IOS -ref-> MyApp.Logic -ref-> MyApp.Interface <-ref- MyApp.Server