खोज…


टिप्पणियों

Xamarin.Forms पर नेविगेशन दो प्रमुख नेविगेशन पैटर्न पर आधारित है: पदानुक्रमित और मोडल।

पदानुक्रमित पैटर्न उपयोगकर्ता को पृष्ठों के ढेर में नीचे जाने और "बैक" / "अप" बटन दबाने की अनुमति देता है।

मोडल पैटर्न एक रुकावट पृष्ठ है जिसमें उपयोगकर्ता से एक विशिष्ट कार्रवाई की आवश्यकता होती है, लेकिन सामान्य रूप से रद्द बटन दबाकर रद्द किया जा सकता है। कुछ उदाहरण सूचना, अलर्ट, संवाद बॉक्स और रजिस्टर / संस्करण पृष्ठ हैं।

दृश्य मॉडल से इनवेशन का उपयोग करना

पहला कदम नेविगेशन इंटरफ़ेस है जिसे हम दृश्य मॉडल पर उपयोग करेंगे:

public interface IViewNavigationService
{
    void Initialize(INavigation navigation, SuperMapper navigationMapper);
    Task NavigateToAsync(object navigationSource, object parameter = null);
    Task GoBackAsync();
}

Initialize मेथड में मैं अपने कस्टम मैपर का उपयोग करता हूँ जहाँ मैं संबंधित कुंजियों के साथ पृष्ठों के प्रकारों का संग्रह रखता हूँ।

public class SuperMapper
{
    private readonly ConcurrentDictionary<Type, object> _typeToAssociateDictionary = new ConcurrentDictionary<Type, object>();

    private readonly ConcurrentDictionary<object, Type> _associateToType = new ConcurrentDictionary<object, Type>();

    public void AddMapping(Type type, object associatedSource)
    {
        _typeToAssociateDictionary.TryAdd(type, associatedSource);
        _associateToType.TryAdd(associatedSource, type);
    }

    public Type GetTypeSource(object associatedSource)
    {
        Type typeSource;
        _associateToType.TryGetValue(associatedSource, out typeSource);

        return typeSource;
    }

    public object GetAssociatedSource(Type typeSource)
    {
        object associatedSource;
        _typeToAssociateDictionary.TryGetValue(typeSource, out associatedSource);

        return associatedSource;
    }
}

पृष्ठों के साथ Enum:

public enum NavigationPageSource
{
    Page1,
    Page2
}

App.cs फ़ाइल:

public class App : Application
{
    public App()
    {
        var startPage = new Page1();
        InitializeNavigation(startPage);
        MainPage = new NavigationPage(startPage);
    }

    #region Sample of navigation initialization
    private void InitializeNavigation(Page startPage)
    {
        var mapper = new SuperMapper();
        mapper.AddMapping(typeof(Page1), NavigationPageSource.Page1);
        mapper.AddMapping(typeof(Page2), NavigationPageSource.Page2);

        var navigationService = DependencyService.Get<IViewNavigationService>();
        navigationService.Initialize(startPage.Navigation, mapper);
    } 
    #endregion
}

मैपर में मैंने एनुम वैल्यू के साथ कुछ पेज टाइप किए।

IViewNavigationService कार्यान्वयन:

[assembly: Dependency(typeof(ViewNavigationService))]
namespace SuperForms.Core.ViewNavigation
{
    public class ViewNavigationService : IViewNavigationService
    {
        private INavigation _navigation;
        private SuperMapper _navigationMapper;

        public void Initialize(INavigation navigation, SuperMapper navigationMapper)
        {
            _navigation = navigation;
            _navigationMapper = navigationMapper;
        }

        public async Task NavigateToAsync(object navigationSource, object parameter = null)
        {
            CheckIsInitialized();

            var type = _navigationMapper.GetTypeSource(navigationSource);

            if (type == null)
            {
                throw new InvalidOperationException(
                    "Can't find associated type for " + navigationSource.ToString());
            }

            ConstructorInfo constructor;
            object[] parameters;

            if (parameter == null)
            {
                constructor = type.GetTypeInfo()
                                  .DeclaredConstructors
                                  .FirstOrDefault(c => !c.GetParameters().Any());

                parameters = new object[] { };
            }
            else
            {
                constructor = type.GetTypeInfo()
                                  .DeclaredConstructors
                                  .FirstOrDefault(c =>
                                    {
                                        var p = c.GetParameters();
                                        return p.Count() == 1 &&
                                            p[0].ParameterType == parameter.GetType();
                                    });

                parameters = new[] { parameter };
            }

            if (constructor == null)
            {
                throw new InvalidOperationException(
                    "No suitable constructor found for page " + navigationSource.ToString());
            }

            var page = constructor.Invoke(parameters) as Page;

            await _navigation.PushAsync(page);
        }

        public async Task GoBackAsync()
        {
            CheckIsInitialized();

            await _navigation.PopAsync();
        }

        private void CheckIsInitialized()
        {
            if (_navigation == null || _navigationMapper == null)
                throw new NullReferenceException("Call Initialize method first.");
        }
    }
}

मुझे उस प्रकार का पेज मिलता है जिस पर उपयोगकर्ता नेविगेट करना चाहता है और प्रतिबिंब का उपयोग करके इसका उदाहरण बनाता है।

और तब मैं दृश्य मॉडल पर नेविगेशन सेवा का उपयोग कर सकता था:

var navigationService = DependencyService.Get<IViewNavigationService>();
await navigationService.NavigateToAsync(NavigationPageSource.Page2, "hello from Page1");


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow