我的javascript路径在此页面上运行: http:// localhost:53049 /
但不在此页面上: http:// localhost:53049/Home/Messages
原因是相对路径不同,前者需要("js/...")而后者需要("../../ js/...").
我在我的Site.Master文件中包含了我的javascript:
<script type="text/javascript" src="js/jquery.jqGrid.js"></script>
<script type="text/javascript" src="~/js/jquery.jqGrid.js"></script>
<script type="text/javascript" src="<%= this.ResolveClientUrl("~/Scripts/jquery-1.2.6.js") %>"></script>
Run Code Online (Sandbox Code Playgroud)
我如何解决这个相对路径的疯狂,即ASP.NET MVC在Site.Master中设置CSS/Javascript路径的最佳实践方式是什么,这样无论视图的URL多么深,它们都适用于每个视图.
附录:
似乎对于Index视图,任何路径都可以工作,奇怪的是:
<script type="text/javascript" src="/Scripts/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../../Scripts/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../../../Scripts/jquery-1.2.6.js"></script>
Run Code Online (Sandbox Code Playgroud)
但对于任何其他页面(具有更深URL的页面),这些都不起作用.
这里发生了什么?我们如何在Site.Master中设置一次Javascript路径,它们适用于所有页面?
ADDENUM II:
事实证明只有jqgrid javascript文件(而不是jquery文件)的问题,显然在该文件中它引用其他javascript文件并且混淆:
<script type="text/javascript" src="<%= Url.Content ("~/js/jquery.jqGrid.js") %>"></script>
Run Code Online (Sandbox Code Playgroud) 我的一位同事重构了这段代码:
private void btnGeneral_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)e.OriginalSource;
Type type = this.GetType();
Assembly assembly = type.Assembly;
string userControlFullName = String.Format("{0}.{1}", type.Namespace, button.Name);
UserControl userControl = (UserControl)assembly.CreateInstance(userControlFullName);
}
Run Code Online (Sandbox Code Playgroud)
这段代码:
private void btnGeneral_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)e.OriginalSource;
Type type = this.GetType();
Assembly assembly = type.Assembly;
UserControl userControl = (UserControl)assembly.CreateInstance(String.Format("{0}.{1}", type.Namespace, button.Name));
}
Run Code Online (Sandbox Code Playgroud)
如果它只使用一次,你就不需要创建一个变量.
我的回答是,制作曾经使用过的变量是很好的做法,因为它:
反对这种方式的论据"更多代码行","不必要的变量"是使编译器的生活更容易但没有显着的速度或资源节省的参数.
任何人都可以想到一个不应该创建曾经使用过的变量名的情况吗?
当我尝试编译以下代码时,我得到错误"可见性"成员无效,因为它没有合格的类型名称.
我需要更改什么才能在Status = off时使TextBlock消失?
XAML:
<Window x:Class="TestTrigger123345.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBlock Text="This is a sentence.">
<TextBlock.Triggers>
<Trigger Property="{Binding Status}" Value="off">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</TextBlock.Triggers>
</TextBlock>
<TextBlock Text="{Binding Status}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System.Windows;
namespace TestTrigger123345
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = this;
Status = "off";
}
public string Status { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我改为DataTrigger和Dependency Properties,它得到了同样的错误:
XAML:
<Window x:Class="TestTrigger123345.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"> …Run Code Online (Sandbox Code Playgroud) 这段代码:
string[] files = {"test.txt",
"test2.txt",
"notes.txt",
"notes.doc",
"data.xml",
"test.xml",
"test.html",
"notes.txt",
"test.as"};
files.ToList().ForEach(f => Console.WriteLine(
f.Substring(
f.IndexOf('.') + 1,
f.Length - f.IndexOf('.') - 1
)
));
Run Code Online (Sandbox Code Playgroud)
产生这个清单:
txt
txt
txt
doc
xml
xml
html
txt
as
Run Code Online (Sandbox Code Playgroud)
有没有办法建立f.IndexOf('.')一个变量,以便在更复杂的LINQ查询中,我在一个地方定义了这个?
我经常发现自己在foreach循环中执行以下索引计数器混乱,以确定我是否在第一个元素上.有没有一种更优雅的方式在C#中执行此操作,有些类似的东西if(this.foreach.Pass == 1)?
int index = 0;
foreach (var websitePage in websitePages) {
if(index == 0)
classAttributePart = " class=\"first\"";
sb.AppendLine(String.Format("<li" + classAttributePart + ">" +
"<a href=\"{0}\">{1}</a></li>",
websitePage.GetFileName(), websitePage.Title));
index++;
}
Run Code Online (Sandbox Code Playgroud) 我有一个25000行的MySQL表.
这是一个导入的CSV文件,所以我想查看最后十行以确保它导入所有内容.
但是,由于没有ID列,我不能说:
SELECT * FROM big_table ORDER BY id DESC
Run Code Online (Sandbox Code Playgroud)
什么SQL语句会告诉我这个表的最后10行?
表的结构就是这样:
columns are: A, B, C, D, ..., AA, AB, AC, ... (like Excel)
all fields are of type TEXT
Run Code Online (Sandbox Code Playgroud) 我读到JavaScript中的双管道检查变量是否是假的,这undefined是JavaScript中的虚假值,例如
这意味着如果值为falsey(例如0,"",null,undefined(另请参见JavaScript中的所有falsey值)),则将其视为false; 否则它被视为真实.
所以我尝试了这一点,发现undefined确实没有被评估为假,而是抛出一个错误:
let elemContent = document.getElementById('content');
let a = null;
let b = 2;
elemContent.innerHTML += a || 'ok'; // "ok"
elemContent.innerHTML += b || 'ok'; // "2"
elemContent.innerHTML += whatever || 'ok'; // "ERROR: whatever is not defined"
Run Code Online (Sandbox Code Playgroud)
是否是undefinedJavaScript中的虚假价值,或者如何理解这一矛盾?
我有一个700w x 300h的WPF应用程序,可以将它拖到我的大屏幕上的任何地方.
我的应用程序执行时:
MessageBox.Show("Sorry, this function is not yet implemented.");
Run Code Online (Sandbox Code Playgroud)
消息框出现在我的屏幕中间,甚至可能在应用程序本身附近.
如何让我的MessageBox出现在我的应用程序中间?
此代码有效(当ControlType ="dropDown"时,背景为黄色):
<Window x:Class="TestCollapsed.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestCollapsed.Commands"
Title="Main Window" Height="400" Width="800">
<Window.Resources>
<Style x:Key="DropDownStyle" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ControlType}" Value="dropDown">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Visibility="Visible"
Text="This is going to be the dropdown control."
Style="{StaticResource DropDownStyle}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
但是这个代码不无法正常工作(当ControlType ="下拉列表",然后将TextBlock仍然是不可见的):
<Window x:Class="TestCollapsed.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestCollapsed.Commands"
Title="Main Window" Height="400" Width="800">
<Window.Resources>
<Style x:Key="DropDownStyle" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ControlType}" Value="dropDown">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Visibility="Collapsed" …Run Code Online (Sandbox Code Playgroud) c# ×4
wpf ×3
xaml ×2
amazon ×1
api ×1
asp.net-mvc ×1
coding-style ×1
datatrigger ×1
foreach ×1
javascript ×1
linq ×1
mysql ×1
phpmyadmin ×1
refactoring ×1
sql ×1
triggers ×1
undefined ×1
variables ×1