Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Dependency Service Objects

$
0
0

Hi all,
I have a Interface like below

public interface ICalculator
{
        void setNum(int num);

        void getWidth();
}

and i implemented a class in Xamarin.Android using that interface like below

[assembly: Xamarin.Forms.Dependency(typeof(Calculator))]namespace Test.Droid
{
    class Calculator: IClaclulator
    {
        private int base = 0;

        public void setNum(int num)
        {
            base = num;
        }

        public int getBase()
        {
            return base;
        }
    }
}

but when i load this class in MainPage with DependencyService like this code

public partial class MainPage  ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        ICalculator icalculator = DependencyService.Get<ICalculator>(DependencyFetchTarget.GlobalInstance);
        icalculator.setNum(10);
        int result = icalculator.getBase();
    }
}

The result should be 10, but it seems that after setNum(10) the num variable in Calculator class will update but after calling getBase() num variable in class is not in memory and it will return default value which is 0;


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>