-2

Can anyone help me? I created simply project Xamarin Forms with Prism i VS2017 on Android (screen). I used Prism Template Pack. I would like connect project with my WebService. here is a link to screen of all project I have two projects PrismCoursApp and PrismCoursApp.Droid. First project contains SecondPageViewModel.cs where I try use connected WebService (wsMES) but I can't add namespace with PrismCoursApp.Droid. The namespace of project PrismCourseApp.Android is PrismCourseApp.Droid and PrismCourseApp.Android depends on PrismCourseApp.
I could add reference to Web service only in PrismCoursApp.Android project but I would like to use it in SecondPageViewModel.cs in PrismCourseApp.

Can someone tell me what I'm doing wrong? Thanks

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

        }
    }
}
Mirek
  • 75
  • 6
  • If your webservice depends on android-specific code, you cannot use it from your platform-independent assembly. But that's just guessing, unless you post some more info and code about your webservice. – Haukinger Sep 17 '18 at 08:23
  • Thank you for answer. I added my code of Android Project and my Views. Thank you for all suggestions. – Mirek Sep 18 '18 at 15:34
  • I would make the webservice platform-independent. It's a _web_ service, any reason why only android should use it? – Haukinger Sep 19 '18 at 07:59
  • Thanks. I was wondering if the dependency service is good solution in this case but I'm not sure.This aplication use only android but really I have to use the Web Service with iOS too in the next step. What do you think about? If you say platform-independ you think about dependency service or not? it is very important for me to know how to correctly connect web service in the future. Thanks for answer. – Mirek Sep 19 '18 at 18:38
  • I mean "reference only `PrismCoursApp` from your service's project (or even better `MyServiceInterface`) but _not_ anything `...Droid`" – Haukinger Sep 19 '18 at 19:02
  • OK. If I understood correctly, I would make MyServiceInterface in PrisCourseApp and implement it in PrismCourseApp.Android. And in iOS in the future too? Tell me if I misunderstood. Is it good way for correctly use WebService? – Mirek Sep 19 '18 at 20:04
  • In my apps there's a project shared between the different client platforms and another project shared between the client and the server. Android -> ClientPlatform -> ServerInterface <- Server – Haukinger Sep 20 '18 at 05:55
  • Ok, this case I try make the same. Whether the project ServerInterface must be project of Prism template pack for different platforma as others? – Mirek Sep 20 '18 at 10:46
  • I created shared project ServerInterface and I set dependencies. It's working :-) Thank you very much!!! – Mirek Sep 20 '18 at 16:56
  • I've added a summary of this as answer, have fun & happy coding :-) – Haukinger Sep 20 '18 at 18:10

1 Answers1

0

Summarising the discussion in the comments above:

One should not make a web service dependent upon something specific to one of many client platforms. It's preferable to put the service's interface between the server and the part of the client that's shared between the different client implementations.

Say you have a mobile app for Android and IOS. Then you'll have two projects MyApp.Droid and MyApp.IOS for the respective client-specific implementations. Also, there's a project that both of them reference, and that (hopefully) contains most of your app's client-side logic: MyApp.Logic.

Now for the server: you have the MyApp.Server project that implements the service. If you need to define interfaces to communicate between the app and the service (WCF comes to mind), you define a project referenced by both the client-side logic (MyApp.Logic) and the server implementation (MyApp.Server): MyApp.Interface.

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

Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • Last question if You can. I create my app with Prism Template Pack. There is Android, iOS and common project. Could you tell me which project is the best for creating ServerInterface project to define interfaces to communicate between the app and the service? – Mirek Sep 20 '18 at 21:02
  • just create a new project in your solution, there may even be a template/wizard for interfacing asmx services (more of a guess than recent practical experience here :-) ) – Haukinger Sep 21 '18 at 06:07
  • It's OK :-) I added WCF Service Aplication and all working. – Mirek Sep 21 '18 at 15:54