OSG(OpenSceneGraph)的.NET封装1-快速创建基于osgEarth.NET和Fluent.Ribbon的数字地球程序

1.使用VS2019新建WPF窗体程序

NuGet添加Fluent.Ribbon程序包。

NuGet添加Tx.osgEarth程序包。

2.app修改


        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Generic.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Fluent;component/Themes/Themes/Dark.Blue.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

3.修改MainWindow

修改基类为RibbonWindow,添加Ribbon、OsgearthView和StatusBar控件。

<Fluent:RibbonWindow  xmlns:Fluent="urn:fluent-ribbon" x:Class="OsgEarthExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:OsgEarthExample"
        xmlns:wfi="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
        xmlns:toeui="clr-namespace:Tx.osgEarth.UI;assembly=Tx.osgEarth"
                      mc:Ignorable="d"
        Title="腾雪osgEarth演示" Height="450" Width="800" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="311*"/>
            <ColumnDefinition Width="489*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <!--Ribbon-->
            <RowDefinition Height="Auto"></RowDefinition>
            <!--工作窗口-->
            <RowDefinition></RowDefinition>
            <!--StatusBar-->
            <RowDefinition Height="25"></RowDefinition>
        </Grid.RowDefinitions>
        <!--Ribbon-->
        <Fluent:Ribbon Name ="ribbon" Grid.ColumnSpan="2" VerticalAlignment="Center">
            <!--Tabs-->
            <!--TFluent:RibbonTabItem Header="地球">
                <Fluent:RibbonGroupBox Header="视图" IsLauncherVisible="False">
                    <Fluent:Button Header="飞往" />
                </Fluent:RibbonGroupBox>
            </Fluent:RibbonTabItem-->
        </Fluent:Ribbon>
        <!--工作窗口-->
        <wfi:WindowsFormsHost  Grid.Row="1" Grid.ColumnSpan="2" Margin="0,0,0,25" Grid.RowSpan="2">
            <toeui:OsgEarthCtrl x:Name="osgEarthView" Dock="Fill"/>
        </wfi:WindowsFormsHost>
        <!--StatusBar-->
        <Fluent:StatusBar Grid.Row="2" Grid.ColumnSpan="2">
            <Fluent:StatusBarItem Title="Ready"
                          Value="150"
                          HorizontalAlignment="Left">
                <TextBlock x:Name="tbInfo" Text="就绪" />
            </Fluent:StatusBarItem>

            <Separator HorizontalAlignment="Left" />

            <Fluent:StatusBarItem Title="WebSite"
                          HorizontalAlignment="Right"
                          Value="www.tengxuekeji.com " />
        </Fluent:StatusBar>
    </Grid>
</Fluent:RibbonWindow>

到此可以直接运行程序,显示osgEarth窗口了。

4.引入腾雪科技的App插件框架

MianWindow完成接口

public partial class MainWindow : RibbonWindow, IMainWindow, IViewWindow
 #region IWindow

        /// <summary>Window句柄.</summary>
        /// <value>The handle.</value>
        public IntPtr Handle  => ((HwndSource)PresentationSource.FromDependencyObject(this)).Handle;

        /// <summary>图标.</summary>
        /// <value>The icon.</value>
        public object IconImage
        {
            get
            {
                return this.Icon;
            }
            set
            {
                this.Icon = value as ImageSource;
            }
        }

        #endregion

        #region IMainWindow


        /// <summary>Gets the active view window.</summary>
        /// <value>The active view window.</value>
        public object ActiveViewWindow => this;

        /// <summary>更新UI.</summary>
        public void RefreshUI()
        {
            this.InvalidateVisual();
        }

        /// <summary>Occurs when [view window active].</summary>
        public event Action<IViewWindow> ViewWindowActive;

        #endregion

        #region IViewWindow
        /// <summary>
        /// Gets the parent.
        /// </summary>
        /// <value>The parent.</value>
        public object ParentWindow => this.Owner;

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        public object View => osgEarthView;

        #endregion

添加成员

 public AppService AppService { get; private set; }

在构造函数完成初始化

        public MainWindow()
        {
            Instance = this;
            InitAppService();

            InitializeComponent();

            var osgEarthObj = this.osgEarthView.OsgEarthObj;

            var map = osgEarthObj.MapNode.Map;
            map.addLayer(new TileSourceImageLayer() { TileSource = new WebTileSource(MapTileType.TIANDITU_IMG), Name = nameof(MapTileType.TIANDITU_IMG) });
            map.addLayer(new TileSourceImageLayer() { TileSource = new WebTileSource(MapTileType.TIANDITU_CIA), Name = nameof(MapTileType.TIANDITU_CIA) });

            InitRibbon();

            ViewWindowActive?.Invoke(this);
        }

        private void InitRibbon()
        {
            var ribbonLoader = new RibbonLoader(ribbon, AppService);
            ribbonLoader.Tab("地球").Group("视图")
                .AddBotton<FlyToCmd>("飞往", null, null);
            ribbonLoader.Tab("地球").Group("图层")
                .AddBotton<HideOrShowTiandituCiaCmd>("天地图矢量", null, null);
        }

        private void InitAppService()
        {
            AppService = new AppService() { MainWindow = this };
            // 绑定预定义的消息
            AppService.Singleton<MessageProxy>().OnShow =
                delegate(string s, string s1, MsgBoxButtons arg3, MsgBoxIcon arg4)
                {
                    var result = TxDialogResult.Cancel;
                    this.SafeInvoke(() =>
                    {
                        result = (TxDialogResult)MessageBox.Show(this, s, s1, (MessageBoxButton)arg3,
                            (MessageBoxImage)arg4);
                    });
                    return result;
                };
            AppService.Singleton<MessageProxy>().OnShowAlert += delegate(string text, string caption)
            {
                this.SafeInvoke(() => { MessageBox.Show(this, text, caption); });
            };
            AppService.Singleton<ProgressProxy>().OnSetText += caption =>
            {
                this.SafeInvoke(() =>
                {
                    bool bRefresh = false;
                    if (caption != null && tbInfo.Text != caption)
                    {
                        tbInfo.Text = caption;
                        bRefresh = true;
                    }

                    //                     if (bRefresh)
                    //                     {
                    //                         Application.DoEvents();
                    //                     }
                });
            };
            AppService.Singleton<ProgressProxy>().OnSetValue += value =>
            {
                this.SafeInvoke(() =>
                {
                    //                     bool bRefresh = false;
                    //                     if (value > 0 && tsProgressBar.Value != value)
                    //                     {
                    TaskbarManager.Instance.SetProgressValue(value, 100, this.Handle);
                    // 
                    //                         tsProgressBar.Value = value;
                    //                         bRefresh = true;
                    //                     }
                    // 
                    //                     if (bRefresh)
                    //                     {
                    //                         statusStrip.Refresh();
                    //                         Application.DoEvents();
                    //                     }
                });
            };
            AppService.Singleton<ProgressProxy>().OnClear += () =>
            {
                this.SafeInvoke(() =>
                {
                    TaskbarManager.Instance.SetProgressValue(0, 100, this.Handle);

                    tbInfo.Text = "就绪";
                    //tsProgressBar.Value = 0;
                    //statusStrip.Refresh();
                    //Application.DoEvents();
                });
            };
        }

运行效果

 

实例程序地址 北京腾雪科技有限责任公司/OsgExample (gitee.com)

转载请标明作者和出处,有问题请回复或发邮件
作者:田腾
QQ:tian_teng@qq.com
QQ群:469468407

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值