我使用Postal来呈现MVC Razor视图并通过电子邮件发送它们.我有一个我专门为电子邮件视图定义的自定义CSS.目前我将它们包括在内如下:
@Styles.Render("~/Content/EmailStyles.css")
Run Code Online (Sandbox Code Playgroud)
但是,这仅包括样式表的相对链接,这在电子邮件中不起作用:
<link href="/Content/EmailStyles.css" rel="stylesheet"/>
Run Code Online (Sandbox Code Playgroud)
我希望将样式表内联,以便它在电子邮件中正常运行.在MVC视图中呈现基于文件的资源的内容的最佳方法是什么?
考虑以下XAML文件:
<Window x:Class="ExpressionVisualizer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sle="clr-namespace:System.Linq.Expressions;assembly=System.Core"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type sle:BinaryExpression}"/>
<ControlTemplate TargetType="{x:Type ContentControl}"/>
</Window.Resources>
</Window>
Run Code Online (Sandbox Code Playgroud)
这给出了以下编译错误:
添加到IDictionary的所有对象必须具有Key属性或与其关联的其他类型的键.第10行位置10.
如果我x:key向ControlTemplate 添加一个属性,它会编译.但是,我不应该这样做.ControlTemplate使用DictionaryKeyProperty属性进行修饰,该属性将TargetType指定为键属性.因此,只要我为ControlTemplate指定TargetType,就不必指定显式键(类似于我没有在我定义的DataTemplate上指定一个键).
我有第二个与切向相关的问题.如果我在XAML中以这种方式定义ControlTemplate(或者是否指定键),它是否会自动应用于未指定其他模板的ContentControl类型的所有控件,或者我是否必须将ControlTemplate嵌入到样式中要发生这种情况?
我已经定义了以下歧视联盟:
type Expr =
| Con of Num
| Var of Name
| Add of Expr * Expr
| Sub of Expr * Expr
| Mult of Expr * Expr
| Div of Expr * Expr
| Pow of Expr * Expr
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个漂亮的打印功能,如下所示:
let rec stringify expr =
match expr with
| Con(x) -> string x
| Var(x) -> string x
| Add(x, y) -> sprintf "(%s + %s)" (stringify x) (stringify y)
| Sub(x, y) -> sprintf "(%s - …Run Code Online (Sandbox Code Playgroud) 在F#中,您可以first按如下方式定义函数:
let first (x, y) = x
你可以这样称呼它:
first (1, 2)
您还可以根据BCL Tuple类型定义相同的功能:
let first (t:Tuple<_, _ >) = t.Item1
但是,您无法使用先前的语法调用它,否则您将收到以下错误:
error FS0001: The type ''c * 'd' is not compatible with the type 'Tuple<'a,'b>'
相反,您必须执行以下操作:
first (Tuple<_,_>(1, 2))
这很奇怪,因为Tuple在任何一种情况下,编译的F#代码似乎都用来表示它的参数.那么为什么F#编译器告诉我类型不兼容?
为什么这有关系?好吧,基本上我想编写一个带有重载的方法来支持任意长度的元组.使用F#的语法元组是不可能的,因为必须提前知道参数的确切数量.但是,它似乎可以通过使用BCL Tuple类型,因为那些使用TRest技巧来允许任意长度的元组.不幸的是,如果我以这种方式编写我的重载,那么它们将无法使用F#语法元组,这是最终目标.
所以我的问题是:为什么语法元组和BCL元组不兼容?而且,是否有任何编写函数和/或方法的例子在F#中对任意长度的元组进行操作?
具体的应用程序处理我正在编写的基于类型推断的二进制解析库.您可以在此处查看代码.你可以看到我对元组的许多重载,但我不想将它们扩展到一些神奇的数字.
考虑以下程序:
using System;
namespace Test
{
public static class Tests
{
public static void Test<T>(T value)
{
Console.WriteLine(typeof(T).Name);
}
public static void TestReflection(object value)
{
typeof(Tests).GetMethod("Test")
.MakeGenericMethod(value.GetType())
.Invoke(null, new [] { value });
}
public static void TestDynamic(object value)
{
Test((dynamic)value);
}
}
class Program
{
public static void Main()
{
Tests.Test(new MyPublicType());
Tests.Test(new MyPrivateType());
Console.WriteLine();
Tests.TestReflection(new MyPublicType());
Tests.TestReflection(new MyPrivateType());
Console.WriteLine();
Tests.TestDynamic(new MyPublicType());
Tests.TestDynamic(new MyPrivateType());
Console.ReadKey();
}
public class MyPublicType {}
private class MyPrivateType : MyPublicType {}
}
}
Run Code Online (Sandbox Code Playgroud)
结果输出: …
考虑以下废话lambda:
function
| [] -> "Empty list"
| hd::tl -> "Not so empty list"
Run Code Online (Sandbox Code Playgroud)
这很好用.现在我重写如下:
function
| [] -> "Empty list"
| hd::tl & l -> "Not so empty list"
Run Code Online (Sandbox Code Playgroud)
同样,出于无意义的原因(并且我知道我可以通过使用as代替而实现相同的效果&,但这都与代码 - 高尔夫问题有关,这与此问题无关).现在F#编译器告诉我:
警告FS0025:此表达式上的不完整模式匹配.例如,值"[]"可以指示模式未涵盖的情况.
这没有任何意义 - 我明确地处理[]了第一条规则中的情况.我没有看到从第一个功能到第二个功能的变化[]; 函数的第二个规则都不匹配它,但只有第二个函数给出警告.我所做的只是添加一个匹配任何东西的附加模式.
当然,使用空列表调用第二个函数确实成功.
是否有正确的原因发生此警告,或者F#模式验证是否只是有一些怪癖?当使用更高级的模式时,我可以看到有这样的情况,但这似乎是一个非常基本的模式.即使问题一般无法解决,似乎这种类型的情况也足够普遍,值得在编译器中进行特殊处理.
我在F#中定义了一个表达式树结构,如下所示:
type Num = int
type Name = string
type Expr =
| Con of Num
| Var of Name
| Add of Expr * Expr
| Sub of Expr * Expr
| Mult of Expr * Expr
| Div of Expr * Expr
| Pow of Expr * Expr
| Neg of Expr
Run Code Online (Sandbox Code Playgroud)
我希望能够漂亮打印表达式树,所以我做了以下事情:
let (|Unary|Binary|Terminal|) expr =
match expr with
| Add(x, y) -> Binary(x, y)
| Sub(x, y) -> Binary(x, y)
| Mult(x, y) -> Binary(x, y) …Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的类型提供程序,我想在程序集中公开一些预生成的类型,如下所示:
[<TypeProvider>]
type TestProvider() as this =
inherit TypeProviderForNamespaces()
let providedType = ProvidedTypeDefinition(Assembly.GetExecutingAssembly(), "Test", "TypeLib", None)
do let assembly = Assembly.LoadFrom @"C:\Some\Long\Path\TestTypes.dll"
// Get same problem with providedType.AddAssemblyTypesAsNestedTypesDelayed(),
// which is what I really want to use
providedType.AddMember(assembly.GetType("TestTypes.Circle"))
this.AddNamespace("Test", [providedType])
Run Code Online (Sandbox Code Playgroud)
我从另一个项目中使用此提供程序如下:
// Circle member not showing up under TypeLib
type Circle = Test.TypeLib.Circle
let c = Circle()
c.Radius <- 4.
printfn "%f" c.Radius
System.Console.ReadKey() |> ignore
Run Code Online (Sandbox Code Playgroud)
它按预期编译,运行和工作,但由于某种原因,Circle它不会出现在Test.TypeLib的Intellisense列表中.当我将鼠标悬停在Circle 它上面时说A reference to type 'TestType.Circle' in assembly 'TestTypes' was …
我有一个要求,我需要将.NET 4.0项目转换为.NET 3.5项目,其他一切都很好,除了"SmtpClient"到目前为止,我发现.NET 3.5 SmtpClient没有实现IDisposable,而在.NET 4.0中它的确!
下面是在.NET4.0上运行良好但在.NET3.5上运行的代码:
Using MailServer As New SmtpClient(MailServerName)
MailServer.Credentials = New System.Net.NetworkCredential(MailServerUserName, MailServerPassword)
SendMail(MailServer, msgBody, msgSubject, FromEmail, ToEmail)
End Using
Run Code Online (Sandbox Code Playgroud)
任何想法如何使用.NET 3.5(我更喜欢使用"使用"代码块来自动处理对象而不是旧式手动配置)
我正在尝试在F#中创建一个函数,它将某些类型转换为字符串,而不是其他类型.目标是可以传递原语,但不能偶然传递复杂的对象.这是我到目前为止所拥有的:
type Conversions =
static member Convert (value:int) =
value.ToString()
static member Convert (value:bool) =
value.ToString()
let inline convHelper< ^t, ^v when ^t : (static member Convert : ^v -> string) > (value:^v) =
( ^t : (static member Convert : ^v -> string) (value))
let inline conv (value:^v) = convHelper<Conversions, ^v>(value)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我的conv函数得到以下编译时错误:
A unique overload for method 'Convert' could not be determined based on type information
prior to this program point. A type annotation may be needed. …Run Code Online (Sandbox Code Playgroud) 我有关闭saveFileDialog的问题.当我点击'取消'窗口再次出现.这是我的代码:
private void SaveAsItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "untitled";
saveFileDialog1.Filter = "Text (*.txt)|*.txt";
saveFileDialog1.ShowDialog();
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
if (saveFileDialog1.ShowDialog()==DialogResult.Cancel)
{
richTextBox1.Text = "CANCEL";
issaved = false;
}
else
{
issaved = true;
SaveFile.WriteLine(richTextBox1.Text);
}
SaveFile.Close();
}
Run Code Online (Sandbox Code Playgroud) f# ×6
.net ×2
c# ×2
asp.net-mvc ×1
dynamic ×1
idisposable ×1
postal ×1
razor ×1
smtpclient ×1
vb.net ×1
winforms ×1
wpf ×1
xaml ×1