最近想自学一下WPF于是做了个demo,如下图。所有功能都实现了就是打包之后出了点问题很不明白。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最近想自学一下WPF于是做了个demo,如下图。所有功能都实现了就是打包之后出了点问题很不明白。相关的知识,希望对你有一定的参考价值。

Example.UserControls里面做了些自定控件。
Example.Core是类库。
我想问的问题是,为什么把Example.App打包后,Images和Configs里面的配置文件都没有被打包进exe里面呢?需要我怎么来设置呢?网上这方面的详细教程太少了。如果Example.App需要进行设置,是不是Example.UserControls里面也是同样设置?
还有个问题,WPF里面获取目录相对路径不像.net那样用Server.MapPath,那应该怎么做呢?分不多,拜谢了!

看截图,整个解决方案里应该只有EXAMPLE.APP是应用程序项目,其他的看着像是类库和资源的项目,我一般这么写Environment.CurrentDirectory,这个目录就是当前EXE的工作目录,当调试的时候也就是BIN/DEBUG目录,相应需要的引用资源你可以尝试直接放到DEBUG目录下进行引用,希望我的回答对你有帮助。 参考技术A 相对路径 this.HeadImg.Source = new BitmapImage(new Uri(@"/UserHeadFace/default.jpg", UriKind.Relative));

资源文件本来就不会被默认打包进exe文件中,别说wpf, 就是经典winform也一样, 你什么见过自动把图片打包进exe的? 如果想要往exe中放入资源文件, 需要用到资源管理的功能, 操作起来有些烦琐,还不如就使用多个文件. 为什么非要只留一个exe呢?追问

就是因为没有搞过想自己弄一下呢。可以详细指教一下吗?

WPF 斜角border

最近看了一些科技感UI设计,其中很多的按钮都不是常见的圆角边,而是斜角边。查了一下,wpf中好像没有现成的斜角border,网上也没搜到现成的,于是自己写了点时间做了一个,写的较简单,有一些bug(主要是没有去管一些极值情况),但也基本可用了。

下面与大家分享一下代码:

先上效果:

前台代码:

    <Window.Resources>
        <Style TargetType="{x:Type local:BeveledBorder}">
            <Setter Property="Background" Value="#90000000" />
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="Margin" Value="2" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#90FF0000"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <UniformGrid Rows="1" VerticalAlignment="Center" MinHeight="50">

        <local:BeveledBorder CornerRadius="10,0,10,0">
            <TextBlock Text="123" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </local:BeveledBorder>

        <local:BeveledBorder CornerRadius="10,10,10,10"/>

        <local:BeveledBorder CornerRadius="10,20,10,20"/>

        <local:BeveledBorder CornerRadius="0,15,0,15"/>

        <local:BeveledBorder Width="40" Height="40"
                             CornerRadius="10,10,10,10"/>

    </UniformGrid>
View Code

 

后台类:

    /// <summary>
    /// 斜角Border
    /// </summary>
    class BeveledBorder : Decorator
    {
        public Brush BorderBrush
        {
            get { return (Brush)GetValue(BorderBrushProperty); }
            set { SetValue(BorderBrushProperty, value); }
        }

        public static readonly DependencyProperty BorderBrushProperty =
            Border.BorderBrushProperty.AddOwner(typeof(BeveledBorder), new PropertyMetadata(Brushes.Transparent, CommonPropertyChanged));

        public Thickness BorderThickness
        {
            get { return (Thickness)GetValue(BorderThicknessProperty); }
            set { SetValue(BorderThicknessProperty, value); }
        }

        public static readonly DependencyProperty  BorderThicknessProperty = 
            Border.BorderThicknessProperty.AddOwner(typeof(BeveledBorder), new PropertyMetadata(new Thickness(), CommonPropertyChanged));

        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }

        public static readonly DependencyProperty BackgroundProperty =
            Control.BackgroundProperty.AddOwner(typeof(BeveledBorder), new PropertyMetadata(Brushes.Transparent, CommonPropertyChanged));

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            Border.CornerRadiusProperty.AddOwner(typeof(BeveledBorder), new PropertyMetadata(new CornerRadius(), CommonPropertyChanged));


        private static void CommonPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as BeveledBorder)._isrendersizechanged = true;
            (d as BeveledBorder).InvalidateVisual();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            if (_isrendersizechanged)
            {
                _isrendersizechanged = false;
                UpdateGeometry(RenderSize);
            }

            Pen pTop = new Pen(BorderBrush, BorderThickness.Top);
            drawingContext.DrawGeometry(Background, pTop, _currenGeometry);
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            _isrendersizechanged = true;
        }

        private void UpdateGeometry(Size nsize)
        {
            if (_currenGeometry == null)
            {
                _currenGeometry = new PathGeometry();
                _currenGeometry.Figures.Add(new PathFigure());
                _currenGeometry.Figures[0].IsClosed = true;
            }
            else
            {
                _currenGeometry.Figures[0].Segments.Clear();
            }

            if (CornerRadius == null)
            {
  
_currenGeometry.Figures[0].StartPoint = new Point(0, 0);

            _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(nsize.Width, 0)));
                  _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(nsize.Width, nsize.Height)));
                  _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(0, nsize.Height)));

            }
            else
            {
                _currenGeometry.Figures[0].StartPoint = new Point(CornerRadius.TopLeft, 0);

                if (CornerRadius.TopRight <= 0)
                {
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(nsize.Width, 0)));
                }
                else
                {
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(nsize.Width - CornerRadius.TopRight, 0)));
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(nsize.Width, CornerRadius.TopRight)));
                }

                if (CornerRadius.BottomRight <= 0)
                {
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(nsize.Width, nsize.Height)));
                }
                else
                {
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(nsize.Width, nsize.Height - CornerRadius.BottomRight)));
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(nsize.Width - CornerRadius.BottomRight, nsize.Height)));
                }

                if (CornerRadius.BottomLeft <= 0)
                {
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(0, nsize.Height)));
                }
                else
                {
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(CornerRadius.BottomLeft, nsize.Height)));
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(0, nsize.Height - CornerRadius.BottomLeft)));
                }

                if (CornerRadius.TopLeft > 0)
                {
                    _currenGeometry.Figures[0].Segments.Add(ToLineSegment(new Point(0, CornerRadius.TopLeft)));
                }
            }
        }

        private LineSegment ToLineSegment(Point pt)
        {
            return new LineSegment(pt, true);
        }


        private PathGeometry _currenGeometry = null;
        private bool _isrendersizechanged = true;
    }

以上是关于最近想自学一下WPF于是做了个demo,如下图。所有功能都实现了就是打包之后出了点问题很不明白。的主要内容,如果未能解决你的问题,请参考以下文章

我去年码了个表(WPF MvvM)

WPF 简易手风琴 (ListBox+Expander)

NodeJS使用formidable实现文件上传

PIXI.JS 基于parcel 打包es6 class语法 做了个流星雨

最近用VC6.0做了一个串口软件,想要美化一下界面,该怎么做啊?用WPF可以么?还是要用其他软件?

被玩坏的博客园之canvas装饰博客园侧边栏