簡體   English   中英

NavigationService在Windows Phone 8上的C#中拋出AccessViolationException

[英]NavigationService throwing AccessViolationException in c# on Windows Phone 8

請記住,我是編程的新手。 我設法將圖像設置為必應地圖上的自定義標記,根據來自JSON提要的數據選擇了圖像並將其放置在地圖上。 我有一個列表視圖頁面,您可以在其中查看洪水警告,單擊此列表中的一項將帶您到另一頁面,其中包含該特定洪水的更多詳細信息。 我想要這樣,以便當有人單擊地圖上的標記時,它將帶他們到與該洪水對應的信息頁面。 (這有點令人費解,但我希望這很清楚)

我很難做到這一點,從列表視圖導航到信息頁面的代碼非常簡單,

private void listBoxBeaches_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        GetSelectedItem();
    }

private void GetSelectedItem()
    {
        RootObject item = listBoxBeaches.SelectedItem as RootObject;

        ArDe = item.AreaDescription;
        SeTe = item.SeverityTxt;
        Rais = item.RaisedF;
        MesEng = item.MessageEnglish;
        MesCym = item.MessageWelsh;

        if (item != null)
        {    
        NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
        }
    }

但是,在嘗試使標記可點擊以使其導航到同一位置時,出現了問題,

    public string ArDe;
    public string SeTe;
    public string Rais;
    public string MesEng;
    public string MesCym;
    public Grid marker;
    public NavigationService navServ;


    private Map myMap = new Map();

    private MapLayer mylayer = new MapLayer();

public Map SetMapPins(List<RootObject>FloodList)
    {
        myMap.LandmarksEnabled = true;
        myMap.PedestrianFeaturesEnabled = true;
        myMap.Center = new GeoCoordinate(52.44, -4);
        myMap.ZoomLevel = 7.8;
        myMap.CartographicMode = MapCartographicMode.Road;

        foreach (var flood in FloodList)
        {
            //this grabs the marker graphic
            marker = flood.GetGrid();

            MapOverlay myOverlay = new MapOverlay();

            myOverlay.GeoCoordinate = new GeoCoordinate(flood.Center.Latitude, flood.Center.Longitude);

            string AreaDes = flood.AreaDescription;
            string SeverityTxt = flood.SeverityTxt;
            string Raised = flood.Raised;
            string EngMessage = flood.MessageEnglish;
            string CymMessage = flood.MessageWelsh;
            marker.MouseLeftButtonUp += (sender, args) => Floodpic_MouseLeftButtonUp(null, null, AreaDes, SeverityTxt, Raised, EngMessage, CymMessage);
            myOverlay.Content = marker;

            mylayer.Add(myOverlay);
        }

        myMap.Layers.Add(mylayer);


        return myMap;
    }

private void Floodpic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e, string AreaDes, string SeverityTxt, string Raised, string EngMessage, string CymMessage)
    {
       GetSelectedMapItem(AreaDes, SeverityTxt, Raised, EngMessage, CymMessage);
    }

public void GetSelectedMapItem(string AreaDes, string SeverityTxt, string Raised, string EngMessage, string CymMessage)
    {
        ArDe = AreaDes;
        SeTe = SeverityTxt;
        Rais = Raised;
        MesEng = EngMessage;
        MesCym = CymMessage;

        //initially I used NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
        //but this gives me "An object reference is required for the non-static field method or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)" error
        Navigate(navServ, new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}", ArDe, SeTe, Rais, MesEng, MesCym), UriKind.Relative));

    }

public void Navigate(NavigationService s, Uri destination)
    {
        //This is where the AccessViolationException is thrown
        s.Navigate(destination);
    }

顯然,listview代碼是從實際的listview頁面(ListView.xaml.cs)導航的,而標記代碼不在我正在導航的頁面的cs文件中(在SetMap.cs中,而不是MapView中)。 xaml.cs,即地圖和標記所在的位置),即它在外部進行導航。

所以我不確定該怎么做,我創建了Navigation方法,因為獲取了對象引用是必需的錯誤

NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));

即使嘗試

this.NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));

現在,在調用Navigate時引發了AccessViolationException異常。 有任何想法嗎?

編輯
我現在已經尋求了一個更簡單的解決方案(使用CustomMessageBox,它可以完成工作),但是我仍然非常感謝對此的解決方案。 我了解這可能是一個非常具體的問題,因此可能需要同樣具體的答案。 該代碼有點混亂,但這是由於我缺乏培訓或經驗。

如果您的頁面位於根目錄,請嘗試此路徑。

NavigationService.Navigate(new Uri(string.Format("~/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));

您不能在構造函數中調用NavigationService.Navigate

或者,如果您正在從usercontrol導航。

RootFrame.Navigate(new Uri(string.Format("~/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));

暫無
暫無

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

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