简体   繁体   中英

Access ListView in xaml from different .cs files?

I am trying to access a ListView object in a XAML file from a non-bindable .cs file.

I have a XAML file called "TabPage1" where I have a grid layout and that is where my listview is located. https://gyazo.com/1a4d8635ffcb5acdd291bc8f92caae88

However, I am receiving shared data from share option in android through an "activity" .cs file called "RecieveDataFromApp, this is where I handle the incoming data. (shown in the picture above on the left side)

From this .cs file (RecieveDataFromApp) I want to pass in incoming data to this ListView that is located in a xaml file which is not bound to my "RecieveDataFromApp.cs" file.

https://gyazo.com/80fff3df19e19e5dbb1c5bc8e6f54995 if you check at line 35, this is where I want to access the ListView that is in TabPage1.xaml.

You need to use DependencyService to have values from Android project into shared project

Create Interface in shared project let call it IGetSmth

 public interface IGetSmth
{
    string smth { get; }

}

In you android project create class named GetSmth

[assembly: Xamarin.Forms.Dependency(typeof(GetSmth))]
public class Getsmth : IGetSmth
{
    public string smth
    {
        get { return ReceiveDataFromApp.SomeTextInMainActivity; }
    }
}

Finaly in you ReceiveDataFromApp activity create SomeTextInMainActivity property
public static string SomeTextInMainActivity { get; set; } public static string SomeTextInMainActivity { get; set; } and don't forget to give it a value in you code and you can get it in shared project string SomeTextFromAndroidProject = DependencyService.Get<IGetSmth>().smth; You can find sample here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM