Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
Loading uri from OpenFileDialog
posted

Hi, i'm trying to write simple application in WPF which can open shp files add it to layers and display them (with possiblity to disable/enable particular layers), but i have big problem with adding layers after boot...

Example 1 (this code don't render file):

        private string filename=null;

        public MainWindow()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            // Create OpenFileDialog

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

 

            // Set filter for file extension and default file extension

            dlg.DefaultExt = ".shp";

            dlg.Filter = "Text documents (.shp)|*.shp";

 

            // Display OpenFileDialog by calling ShowDialog method

            Nullable<bool> result = dlg.ShowDialog();

 

            // Get the selected file name and display in a TextBox

            if (result == true)

            {

                filename = Path.GetDirectoryName(dlg.FileName) + "\\" + Path.GetFileNameWithoutExtension(dlg.FileName);

            }

            MapLayer WorldLayer = new MapLayer();

            ShapeFileReader reader = new ShapeFileReader();

            reader.Uri = filename;

            WorldLayer.Reader = reader;

            WorldLayer.ImportAsync();

            xamMap1.Layers.Add(WorldLayer);

            MapNavigationPane navPane = new MapNavigationPane();

            navPane.SetValue(XamDock.EdgeProperty, DockEdge.InsideRight);

            navPane.Margin = new Thickness(10);

            xamMap1.LogicalChildren.Add(navPane);

        }

    }

Example 2 (this code renders file):

        private string filename=null;

        public MainWindow()

        {

            InitializeComponent();

            // Create OpenFileDialog

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

 

            // Set filter for file extension and default file extension

            dlg.DefaultExt = ".shp";

            dlg.Filter = "Text documents (.shp)|*.shp";

 

            // Display OpenFileDialog by calling ShowDialog method

            Nullable<bool> result = dlg.ShowDialog();

 

            // Get the selected file name and display in a TextBox

            if (result == true)

            {

                filename = Path.GetDirectoryName(dlg.FileName) + "\\" + Path.GetFileNameWithoutExtension(dlg.FileName);

            }

            MapLayer WorldLayer = new MapLayer();

            ShapeFileReader reader = new ShapeFileReader();

            reader.Uri = filename;

            WorldLayer.Reader = reader;

            WorldLayer.ImportAsync();

            xamMap1.Layers.Add(WorldLayer);

            MapNavigationPane navPane = new MapNavigationPane();

            navPane.SetValue(XamDock.EdgeProperty, DockEdge.InsideRight);

            navPane.Margin = new Thickness(10);

            xamMap1.LogicalChildren.Add(navPane);

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

        }

    }

And here is XAML file in both cases the same:

         <Window x:Class="testwczytywania.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525"

        xmlns:ig="http://schemas.infragistics.com/xaml">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="26*" />

            <RowDefinition Height="285*" />

        </Grid.RowDefinitions>

        <Button Content="Button" Height="23" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

        <Grid Grid.Row="1">

                <ig:XamMap HorizontalAlignment="Left" Margin="11,37,0,0" Name="xamMap1" VerticalAlignment="Top" MinHeight="299" MinWidth="491" />

        </Grid>

    </Grid>

</Window>

Is there any possiblity to render shp when you don't know uri until runtime?
Best Regards
Morpheus

  • 3255
    posted

    Hi Morpheus,

    Try the following: add the WorldLayer to XamMap's Layer collection and then call ImportAsync. e.g.:

    xamMap1.Layers.Add(WorldLayer);
    WorldLayer.ImportAsync(); 

    Regards,

    Ivan Kotev