ChildWindow/ChildWindowStyle

一、ChildWindow

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WindowsBase
{
    /// <summary>
    /// 子窗体模板
    /// </summary>
    public class ChildWindow : Window, INotifyPropertyChanged
    {

        ResourceDictionary style1;
        ControlTemplate childWindowTemplate;
        static ChildWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ChildWindow), new FrameworkPropertyMetadata(typeof(ChildWindow)));
        }
        public ChildWindow()
        {
            this.DataContext = this;
            style1 = new ResourceDictionary();
            style1.Source = new Uri("InstallPackageWPF;component/WindowsBase/ChildWindowStyle.xaml", UriKind.Relative);
            this.Style = (System.Windows.Style)style1["ChildWindowStyle"];
        }

        public override void OnApplyTemplate()
        {
            childWindowTemplate = (ControlTemplate)style1["ChildWindowTemplate"];

            Border borderTitle = (Border)childWindowTemplate.FindName("borderTitle", this);

            borderTitle.MouseMove += delegate (object sender, MouseEventArgs e)
            {
                WindowMove(e);
            };
            Button minBtn = (Button)childWindowTemplate.FindName("btnMin", this);
            minBtn.Click += delegate
            {
                this.WindowState = WindowState.Minimized;
            };
            Button closeBtn = (Button)childWindowTemplate.FindName("btnClose", this);

            closeBtn.Click += delegate
            {
                this.Close();
            };
        }
        public void WindowMove(MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                 this.DragMove();
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected PropertyChangedEventHandler PropertyChangedHandler
        {
            get
            {
                return PropertyChanged;
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        protected PropertyChangingEventHandler PropertyChangingHandler
        {
            get
            {
                return PropertyChanging;
            }
        }

        public void VerifyPropertyName(string propertyName)
        {
            var myType = GetType();

            if (!string.IsNullOrEmpty(propertyName)
                && myType.GetProperty(propertyName) == null)
            {
                var descriptor = this as ICustomTypeDescriptor;

                if (descriptor != null)
                {
                    if (descriptor.GetProperties()
                        .Cast<PropertyDescriptor>()
                        .Any(property => property.Name == propertyName))
                    {
                        return;
                    }
                }

                throw new ArgumentException("Property not found", propertyName);
            }
        }
        public virtual void RaisePropertyChanging(
            string propertyName)
        {
            VerifyPropertyName(propertyName);

            var handler = PropertyChanging;
            if (handler != null)
            {
                handler(this, new PropertyChangingEventArgs(propertyName));
            }
        }
        public virtual void RaisePropertyChanged(
           string propertyName)
        {
            VerifyPropertyName(propertyName);

            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected bool Set<T>(
           string propertyName,
           ref T field,
           T newValue)
        {
            if (EqualityComparer<T>.Default.Equals(field, newValue))
            {
                return false;
            }

            RaisePropertyChanging(propertyName);
            field = newValue;

            RaisePropertyChanged(propertyName);

            return true;
        }
        protected bool Set<T>(
            ref T field,
            T newValue,
             string propertyName = null)
        {
            return Set(propertyName, ref field, newValue);
        }
    }
}

二、ChildWindowStyle

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- - -->
    <PathGeometry x:Key="pathMin">M512-359.489906M498.081951-47.76654M772.963422 362.508895l-528.06716 0c-12.38297 0-22.514491 10.131521-22.514491 22.514491l0 0c0 12.38297 10.131521 22.514491 22.514491 22.514491l528.06716 0c12.38297 0 22.514491-10.131521 22.514491-22.514491l0 0C795.477913 372.640416 785.346392 362.508895 772.963422 362.508895z</PathGeometry>
    <!-- x -->
    <PathGeometry x:Key="pathClose">M540.672 384l303.104 303.104c8.192 8.192 8.192 20.48 0 28.672s-20.48 8.192-28.672 0l-303.104-303.104L208.896 715.776c-8.192 8.192-20.48 8.192-28.672 0s-8.192-20.48 0-28.672l303.104-303.104-303.104-303.104c-8.192-8.192-8.192-20.48 0-28.672 4.096-4.096 8.192-4.096 16.384-4.096s12.288 0 16.384 4.096l303.104 303.104 303.104-303.104c4.096-4.096 8.192-4.096 16.384-4.096s12.288 0 16.384 4.096c8.192 8.192 8.192 20.48 0 28.672L540.672 384z</PathGeometry>
    <!--  Window模板  -->
    <ControlTemplate x:Key="ChildWindowTemplate" TargetType="{x:Type Window}">
        <ControlTemplate.Resources>
            <Storyboard x:Key="closeStoryboard">
                <DoubleAnimation
                    AutoReverse="False"
                    Storyboard.TargetProperty="RenderTransform.ScaleX"
                    From="1"
                    To="0.8"
                    Duration="0:0:0.3" />
                <DoubleAnimation
                    AutoReverse="False"
                    Storyboard.TargetProperty="RenderTransform.ScaleY"
                    From="1"
                    To="0.8"
                    Duration="0:0:0.3" />
                <DoubleAnimation
                    AutoReverse="False"
                    Storyboard.TargetProperty="Opacity"
                    From="1"
                    To="0.8"
                    Duration="0:0:0.3" />
            </Storyboard>
        </ControlTemplate.Resources>
        <Grid
            Name="WindowGrid"
            Background="Transparent"
            RenderTransformOrigin="0.5,0.5">
            <Grid.RenderTransform>
                <ScaleTransform />
            </Grid.RenderTransform>
            <Border
                Width="{TemplateBinding Width}"
                Height="{TemplateBinding Height}"
                Background="{TemplateBinding Background}"
                CornerRadius="4">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="44" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Border Name="borderTitle" Panel.ZIndex="10">
                        <Grid Background="Transparent">
                            <StackPanel HorizontalAlignment="Right"
                                    VerticalAlignment="Top"
                                    Orientation="Horizontal" Margin="0,10,20,0" Panel.ZIndex="1">
                                <Button x:Name="btnMin" Foreground="White" Width="14" Height="14" Margin="0,0,0,10" Style="{DynamicResource PathButtonStyle}">
                                    <Path Data="{DynamicResource pathMin}"></Path>
                                </Button>
                                <!--<Button x:Name="btnMax" Focusable="False" Width="16" Height="16" />-->
                                <Button x:Name="btnClose" Foreground="White" Width="14" Margin="15,0,0,0" Height="14" Style="{DynamicResource PathButtonStyle}">
                                    <Path Data="{DynamicResource pathClose}"></Path>
                                </Button>
                            </StackPanel>
                        </Grid>
                    </Border>
                    <!--  内容  -->
                    <Border
                        Grid.RowSpan="2"
                        Width="Auto"
                        Height="Auto">
                        <AdornerDecorator>
                            <ContentPresenter />
                        </AdornerDecorator>
                    </Border>
                </Grid>
            </Border>
        </Grid>
    </ControlTemplate>
    <Style x:Key="ChildWindowStyle" TargetType="{x:Type Window}">
        <Setter Property="AllowsTransparency" Value="True" />
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="ResizeMode" Value="NoResize" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template" Value="{StaticResource ChildWindowTemplate}" />
    </Style>
</ResourceDictionary>

三、使用

   public partial class MainWindow : ChildWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
     }
<template> <div class="container"> <div class="preview-content" v-if="fileLink"> <iframe :src="fileLink" frameborder="0" class="preview-content-iframe"></iframe> </div> <div v-if="fileLink.value === null" style="color: green; font-weight: bold; background: #e6ffe6; padding: 10px; border: 1px solid #b2ffb2;"> 该流程已成功处理 </div> <div class="error-message" v-else> 文件评审链接加载失败,请检查网络或数据。 </div> </div> </template> <script> import { ref, onMounted } from 'vue' import { useRoute } from 'vue-router' import axios from 'axios' export default { setup() { const route = useRoute() const fileLink = ref(null) const fetchPreviewLink = async () => { try { const tableId = route.query.TableId if (!tableId) { console.warn('缺少参数 TableId') return } debugger // 获取文件信息 const fileRes = await axios.get('/api/Flow_Task/getFile', { params: { TableId: tableId } }) const fileData = fileRes.data debugger if (!fileData) { console.warn('未找到文件信息') return } const flowInstanceID = fileData.result.reviewInfo.id const count = fileData.result.count console.log(flowInstanceID) console.log(count) debugger // 获取流程ID const flowIdRes = await axios.get('/api/Flow_Task/getFlowID', { params: { flowInstanceID } }) const sFlowID = flowIdRes.data.flowID console.log(sFlowID) debugger if(count == 1){ if (sFlowID === 'fileBatchFlow') { const encodedReviewId = encodeURIComponent(flowInstanceID) const targetUrl = `/#/Drawings_Online_Approval_A?reviewId=${encodedReviewId}` fileLink.value = targetUrl window.open(targetUrl, '_blank') } else if(sFlowID === 'fileReviewFlow'){ debugger // 获取当前用户工号 const workNumRes = await axios.post('/api/Review_Info/getCurrentUserWorkNum') const sWorkNum = workNumRes.data.sWorkNum // 构造跳转 URL const sFileCode = encodeURIComponent(fileData.result.reviewInfo.fileCode) const sReviewVersion = encodeURIComponent(fileData.result.reviewInfo.fileVersion) const targetUrl = `http://10.9.38.83:5773/#/document-review?sFileCode=${sFileCode}&sWorkNum=${encodeURIComponent( sWorkNum )}&sReviewVersion=${sReviewVersion}` fileLink.value = targetUrl window.open(targetUrl, '_blank') } } else{ // 获取taskid const Task = await axios.get('/api/Flow_Task/getFlowInsTask', { params: { TableId: tableId } }) const task = Task.data const taskid = task.flow_Task.id const taskflowid = task.flow_Task.flowInstanceID const url = `/#/File_Approval_Flow/${taskflowid}/${taskid}?flowID=${"fileApprovalFlow"}`; console.log('即将打开的路径:', url); // 打印路径 fileLink.value = url const childWindow = window.open(url, '_blank') window.childWindow = childWindow // 挂到全局变量供子窗口访问 } } catch (error) { console.error('处理跳转逻辑失败:', error) alert('处理跳转逻辑失败,请查看控制台日志') } } onMounted(() => { fetchPreviewLink() // 监听来自新窗口的消息 window.addEventListener('message', (event) => { console.log(event.data) debugger if (event.data === 'closeIframe') { fileLink.value = null window.childWindow.close() debugger } }) }) return { fileLink } } } </script> <style lang="less" scoped> .container { display: flex; width: 100%; height: 100%; flex-direction: row; flex-wrap: wrap; border-right: 1px solid rgb(182, 197, 240); border-bottom: 1px solid rgb(182, 197, 240); .error-message { font-size: 24px; color: #d90000; text-align: center; padding: 20px; width: 100%; height: 100%; } .preview-content { width: 100%; height: 100%; margin-top: 5px; border: 1px solid #dfeafc; .preview-content-iframe { width: 100%; height: 100%; } } } </style> 逻辑没问题吧
10-23
<template> <div class="container"> <div class="preview-content" v-if="fileLink"> <iframe :src="fileLink" frameborder="0" class="preview-content-iframe"></iframe> </div> <div class="error-message" v-else> 该流程已评审或审批或链接加载失败,请检查网络或数据。 </div> </div> </template> <script> import { ref, onMounted } from 'vue' import { useRoute } from 'vue-router' import axios from 'axios' export default { setup() { const route = useRoute() const fileLink = ref(null) const fetchPreviewLink = async () => { try { const tableId = route.query.TableId if (!tableId) { console.warn('缺少参数 TableId') return } // 获取当前用户工号 const workNumRes1 = await axios.post('/api/Review_Info/getCurrentUserWorkNum') const sWorkNum1 = workNumRes1.data.sWorkNum const flowtask = await axios.get('/api/Flow_Task/getFlowInsTask1', { params: { TableId: tableId,id: sWorkNum1} }) if(flowtask.data.flow_Task != null){ // 获取taskid const Task = await axios.get('/api/Flow_Task/getFlowInsTask', { params: { TableId: tableId } }) const task = Task.data const taskid = task.flow_Task.id const taskflowid = task.flow_Task.flowInstanceID const url = `/#/File_Approval_Flow/${taskflowid}/${taskid}?flowID=${"fileApprovalFlow"}`; console.log('即将打开的路径:', url); // 打印路径 fileLink.value = url const childWindow = window.open(url, '_blank') window.childWindow = childWindow // 挂到全局变量供子窗口访问 }else{ // 获取是否存在历史数据 const flowHistory = await axios.get('/api/Flow_Task/getFlowTaskHistory', { params: { TableId: tableId,id: sWorkNum1} }) if(flowHistory.data.flowTaskHistory != null){ console.log(flowHistory) debugger if(flowHistory?.data?.flowTaskHistory?.node === '评委'){ // 获取文件信息 debugger const fileRes = await axios.get('/api/Flow_Task/getFile', { params: { TableId: tableId } }) const fileData = fileRes.data debugger if (!fileData) { console.warn('未找到文件信息') return } const flowInstanceID = fileData.result.reviewInfo.id const count = fileData.result.count console.log(flowInstanceID) console.log(count) debugger // 获取流程ID const flowIdRes = await axios.get('/api/Flow_Task/getFlowID', { params: { flowInstanceID } }) const sFlowID = flowIdRes.data.flowID console.log(sFlowID) debugger if (sFlowID === 'fileBatchFlow') { const encodedReviewId = encodeURIComponent(flowInstanceID) const targetUrl = `/#/Drawings_Online_Approval_A?reviewId=${encodedReviewId}` fileLink.value = targetUrl window.open(targetUrl, '_blank') } else if(sFlowID === 'fileReviewFlow'){ debugger // 获取当前用户工号 const workNumRes = await axios.post('/api/Review_Info/getCurrentUserWorkNum') const sWorkNum = workNumRes.data.sWorkNum // 构造跳转 URL const sFileCode = encodeURIComponent(fileData.result.reviewInfo.fileCode) const sReviewVersion = encodeURIComponent(fileData.result.reviewInfo.fileVersion) const targetUrl = `http://10.9.38.83:5773/#/document-review?sFileCode=${sFileCode}&sWorkNum=${encodeURIComponent( sWorkNum )}&sReviewVersion=${sReviewVersion}` fileLink.value = targetUrl window.open(targetUrl, '_blank') } }else{ const fallbackResponse1 = await axios.get('/api/Flow_Task/getFlowIns', { params: { TableId: route.query.TableId } }) debugger console.log(fallbackResponse1.data); const taskflowid2 = fallbackResponse1.data.flow_Instance.id console.log(taskflowid2) debugger const url2 = `/#/File_Approval_Flow/${taskflowid2}?flowID=${"fileApprovalFlow"}`; console.log('即将打开的路径:', url2); // 打印路径 fileLink.value = url2 } }else{ debugger // 获取文件信息 const fileRes = await axios.get('/api/Flow_Task/getFile', { params: { TableId: tableId } }) const fileData = fileRes.data debugger if (!fileData) { console.warn('未找到文件信息') return } const flowInstanceID = fileData.result.reviewInfo.id const count = fileData.result.count console.log(flowInstanceID) console.log(count) debugger // 获取流程ID const flowIdRes = await axios.get('/api/Flow_Task/getFlowID', { params: { flowInstanceID } }) const sFlowID = flowIdRes.data.flowID console.log(sFlowID) debugger if(count == 1){ if (sFlowID === 'fileBatchFlow') { const encodedReviewId = encodeURIComponent(flowInstanceID) const targetUrl = `/#/Drawings_Online_Approval_A?reviewId=${encodedReviewId}` fileLink.value = targetUrl window.open(targetUrl, '_blank') } else if(sFlowID === 'fileReviewFlow'){ debugger // 获取当前用户工号 const workNumRes = await axios.post('/api/Review_Info/getCurrentUserWorkNum') const sWorkNum = workNumRes.data.sWorkNum // 构造跳转 URL const sFileCode = encodeURIComponent(fileData.result.reviewInfo.fileCode) const sReviewVersion = encodeURIComponent(fileData.result.reviewInfo.fileVersion) const targetUrl = `http://10.9.38.83:5773/#/document-review?sFileCode=${sFileCode}&sWorkNum=${encodeURIComponent( sWorkNum )}&sReviewVersion=${sReviewVersion}` fileLink.value = targetUrl window.open(targetUrl, '_blank') } } else{ // 获取taskid const Task = await axios.get('/api/Flow_Task/getFlowInsTask', { params: { TableId: tableId } }) const task = Task.data const taskid = task.flow_Task.id const taskflowid = task.flow_Task.flowInstanceID const url = `/#/File_Approval_Flow/${taskflowid}/${taskid}?flowID=${"fileApprovalFlow"}`; console.log('即将打开的路径:', url); // 打印路径 fileLink.value = url const childWindow = window.open(url, '_blank') window.childWindow = childWindow // 挂到全局变量供子窗口访问 } } } catch (error) { console.log(error) debugger if (error) { debugger // 当返回 404 时调用备用接口 try { debugger const fallbackResponse = await axios.get('/api/Flow_Task/getFlowIns', { params: { TableId: route.query.TableId } }) debugger console.log(fallbackResponse.data); const taskflowid1 = fallbackResponse.data.flow_Instance.id console.log(taskflowid1) debugger const url1 = `/#/File_Approval_Flow/${taskflowid1}?flowID=${"fileApprovalFlow"}`; console.log('即将打开的路径:', url1); // 打印路径 fileLink.value = url1 } catch (error) { console.error('备用接口调用失败:'); } } else { console.error('请求异常:', error); } } } } onMounted(() => { fetchPreviewLink() // 监听来自新窗口的消息 window.addEventListener('message', (event) => { console.log(event.data) debugger if (event.data === 'closeIframe') { setTimeout(() => { fileLink.value = null }, 2000) // 2000 毫秒 = 2 秒 window.childWindow.close() debugger } }) }) return { fileLink } } } </script> <style lang="less" scoped> .container { display: flex; width: 100%; height: 100%; flex-direction: row; flex-wrap: wrap; border-right: 1px solid rgb(182, 197, 240); border-bottom: 1px solid rgb(182, 197, 240); .error-message { font-size: 24px; color: #d90000; text-align: center; padding: 20px; width: 100%; height: 100%; } .preview-content { width: 100%; height: 100%; margin-top: 5px; border: 1px solid #dfeafc; .preview-content-iframe { width: 100%; height: 100%; } } } </style>是哪里少括号了吗
最新发布
10-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值