簡體   English   中英

Xamarin Forms with Prism - 使用 WebService 的問題

[英]Xamarin Forms with Prism - Problem with using WebService

誰能幫我? 我在 Android(屏幕)上使用 Prism i VS2017 創建了簡單的 Xamarin Forms 項目。 我使用了棱鏡模板包。 我想將項目與我的 WebService 連接。 這是所有項目的屏幕鏈接我有兩個項目 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

        }
    }
}

總結以上評論中的討論:

不應使 Web 服務依賴於許多客戶端平台之一的特定內容。 最好將服務的接口放在服務器和不同客戶端實現之間共享的客戶端部分之間。

假設您有一個適用於 Android 和 IOS 的移動應用程序。 然后,您將有兩個項目MyApp.DroidMyApp.IOS分別用於特定於客戶端的實現。 此外,還有一個他們都引用的項目,並且(希望)包含您應用程序的大部分客戶端邏輯: MyApp.Logic

現在對於服務器:您擁有實現該服務的MyApp.Server項目。 如果您需要定義接口以在應用程序和服務之間進行通信(想到 WCF),您可以定義一個由客戶端邏輯 ( MyApp.Logic ) 和服務器實現 ( MyApp.Server ) 引用的項目: MyApp.Interface

MyApp.Droid & MyApp.IOS -ref-> MyApp.Logic -ref-> MyApp.Interface <-ref- MyApp.Server

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM